How to store 2D arrays in a for loop inside a parfor loop?
Afficher commentaires plus anciens
I compute e.g. a 3x1 array inside the inner for-loop. In each iteration of this loop, the 3x1 array is saved in a matrix.
At the end of each run of the inner for loop, I wish to then store the nx3 array inside a bigger A array of size (n*m)x3, in order.
Therefore, if total = n*m, for each i , the nx3 array would be saved in the position A(total*(i-1)+1:total*i,:).
Example that is not valid:
n = 1e2;
m = 1e6;
total = n*m;
A = zeros(total,3);
parfor i = 1:n
A_temp = zeros(m,3);
for j = 1:m
A_temp(j,:) = rand(1,3);
end
A(total*(i-1)+1:total,:) = A_temp;
end
Why is this not valid? I think that for any combination of i and j, there wouldn't be an overlap.
What is the correct way to do this?
I have tried using a cell as follows, but then I have to "unwrap" the cell and save it as a matrix, and that takes as much time as the parfor for large values of m.
A_cell = cell(n); % rows of 3 columns
parfor i = 1:n
A_temp = zeros(m,3);
for j = 1:m
A_temp(j,:) = rand(1,3);
end
A_cell(i) = A_temp;
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!