# Save results from nested loop with multiple iterations

Hello everyone!

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

``````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!

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

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.

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

1 Like

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

``````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.
2 Likes