# Lapply with different outputs in Julia

**URL:** https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029
**Category:** New to Julia
**Created:** [January 16, 2023, 2:17pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029 "2023-01-16T14:17:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ditfurth](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@ditfurth](https://discourse.julialang.org/u/ditfurth)
#### Post date: [January 16, 2023, 2:17pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/1 "2023-01-16T14:17:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [January 16, 2023, 2:36pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/2 "2023-01-16T14:36:11Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![ditfurth](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@ditfurth](https://discourse.julialang.org/u/ditfurth)
#### Post date: [January 16, 2023, 2:46pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/3 "2023-01-16T14:46:45Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [January 16, 2023, 2:58pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/4 "2023-01-16T14:58:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 16, 2023, 4:13pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/5 "2023-01-16T16:13:13Z")

</div>

> [@ditfurth](#):
>
> mapslices(x → calc\_mc\_w\_r(x, dt\_m, jacobian), ret; dims = 1)

You can try something like this, with a generator:

```julia
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.

---

<div class="post-metadata">

### Author: ![ditfurth](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@ditfurth](https://discourse.julialang.org/u/ditfurth)
#### Post date: [January 17, 2023, 3:00pm UTC](https://discourse.julialang.org/t/lapply-with-different-outputs-in-julia/93029/6 "2023-01-17T15:00:27Z")

</div>

Thank you!
