# Save results from nested loop with multiple iterations

**URL:** https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412
**Category:** General Usage
**Tags:** loops
**Created:** [January 23, 2023, 3:36pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412 "2023-01-23T15:36:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Filippati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filippati/32/46143_2.png) [@Filippati](https://discourse.julialang.org/u/Filippati)
#### Post date: [January 23, 2023, 3:36pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412/1 "2023-01-23T15:36:35Z")

</div>

Hello everyone!

I am writing a nested loop with a structure that can be summarised with something like this:

```julia
D = []
horizon = 40
for i in 1:7
    A = function1(data[:, i]) # A is a MxN matrix
    B = function2(data[:, i]) # B is a NxT matrix
    C = zeros(size(B))
for h in 2:horizon
   B = A * B # a MxT matrix
   C .+= B # adding up results in C from B
end
  push!(D, C[2, :]') #saving only results from the second row, transpose to save it as a vector
end
D = mapreduce(permutedims, hcat, D) #converting from array of arrays to matrix

```

My problem is that I would like to save the results of `C[2, :]` with the `push!` in a matrix, and not in a vector of vectors `D`, avoiding the `mapreduce` function.  
I tried to look for similar topics but haven’t seen a conversion from array of arrays into a matrix inside a loop.  
I am sorry for the exemplification of the code, but I am new to Julia, please tell me if I can provide further explanations.

Thank you very much in advance for the help!

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [January 23, 2023, 3:50pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412/2 "2023-01-23T15:50:46Z")

</div>

```julia
D = zeros((T,7))
...
D[:, i] .= @view C[2,:]

```

---

<div class="post-metadata">

### Author: ![Filippati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filippati/32/46143_2.png) [@Filippati](https://discourse.julialang.org/u/Filippati)
#### Post date: [January 27, 2023, 10:54am UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412/3 "2023-01-27T10:54:30Z")

</div>

Hey Jeff! Thanks for your reply!

I’m sorry but I would like to define `D` inside the loop, after the first `end` because the dimensionality `T` would be contingent on the data. Against this framework this idea does not work, because at each iteration the matrix `D` goes back to blank.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [January 27, 2023, 3:30pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412/4 "2023-01-27T15:30:19Z")

</div>

Does the required number of rows of `D` (ie. `T`) change in each iteration and you don’t know the maximum number of rows? If so, then I think you can’t get away from what you’re doing. If you know the maximum number of rows, then you could pad your `C[2,:]` to that length, `append!` to a vector `D`, and then `reinterpret` it to a matrix outside the loop. [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.reinterpret). But in that case, you could just allocate `D` as a matrix with the known maximum number of rows, pad `C[2,:]` and assign it `D`.

---

<div class="post-metadata">

### Author: ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)
#### Post date: [January 27, 2023, 4:31pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412/5 "2023-01-27T16:31:11Z")

</div>

What @Jeff_Emanuel says is correct, if you don’t know the size in advance, then you can’t push the results in a matrix. You could build a new matrix in every iteration, e.g.,

```julia
D = [D; permutedims(C[2, :])]

```

But that wouldn’t be very efficient, and you’d need to initialize `D` with `fill(0, 0, w)`, where `w` is the length of your rows.

What you are doing is probably the best you can do, albeit I would recommend to modifications:

1. Use `permutedims` instead of `'` (conjugate transposition), as it would work as expected for strings and complex numbers as well. In this case, however, you don’t even need that, just store `C[2, :]`, which is a vector anyway.
2. Replace the final assignment with this: `D = permutedims(hcat(D...))`. This ends up calling `hcat` only once, which is a lot more efficient.
