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.

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.

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:

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.

Replace the final assignment with this: D = permutedims(hcat(D...)). This ends up calling hcat only once, which is a lot more efficient.