Lapply with different outputs in Julia

Hi all,

I am trying to get mapslices to take a function and run on a vector, like I used to do with lists in R and lapply.

However, there are 2 difficulties:

  1. There are other arguments.
  2. The output of each iteration are matrices with dimension (x,3) where x can vary.

In lapply, it is then easy to aggregate over the outputs (Julia, vcat) across rows to get a (Y,3) matrix, which is my goal.

my current attempt is:
mapslices(x → calc_mc_w_r(x, dt_m, jacobian), ret; dims = 1)

where I am trying to iterate over ‘ret’, having additional arguments ‘dt_m, jacobian’, and my function is calc_mc_w_r.

It gives me the error: ERROR: DimensionMismatch(“tried to assign 2 elements to 1 destinations”).

I am guessing that the output dimension is the error here, the function works just fine if I run e.g. calc_mc_w_r(3, dt_m, jacobian).

Many thanks

You may use broadcasting directly via calc.(ret,Ref(dt_m),Ref(jacobian)) IMHO. A real minimal working example would be needed to go further.

More generally, in Julia, it is often better to avoid vectorized calls and use directly loops: they are easier on the compiler and therefore generally faster (they might avoid allocations of unnecesary arrays, etc.)

1 Like

Ok thank you. I will try it out and get back to you if I run into problems with a real example.

As regards your second point, I could loop over the elements of the vector ‘ret’.
What is the most efficient way to save the results in each iteration? in an ever expanding matrix?

No, usually the efficient way is to pre-allocate the matrix, by creating it with, e.g., zeros((n,m)) or the generic version Array{T}(undef,n,m) if i remember correctly.

But the most efficient way is, when you merge this part of the code with the next part (wich is maybe you do something with the ret vector, this vector is not your output ?), just remove the allocated vector.

1 Like

You can try something like this, with a generator:

reduce(vcat, calc_mc_w_r(x, dt_m, jacobian) for x in eachslice(ret; dims=1))

In terms of efficiency, it should be faster to concatenate horizontally, so this entire thing may be faster if calc_mc_w_r returns an 3xM matrix instead of an Mx3 matrix.

1 Like

Thank you!