Effacer les filtres
Effacer les filtres

use of cell arrays to store matrices... but parfor gives an error

2 vues (au cours des 30 derniers jours)
Michael
Michael le 15 Mar 2013
Hi,
I've got a loop which creates three matrices, and I want to store these for each iteration. Previously I've been saving them as indexed files, but I've just discovered cell arrays which appear to be ideal for this purpose.
My code looks like this:
mat_array = cell(3,50);
parfor i = 1:50
[A,B,C] = matrixgenerator(~);
mat_array {1,i} = A;
mat_array {2,i} = B;
mat_array {3,i} = C;
end
However, I'm getting an error message that mat_array is "indexed in different way, potentially causing dependencies between iterations". I don't understand this error, and don't see where any conflict could arise. Can anyone help me with a workaround?
Thanks

Réponse acceptée

Edric Ellis
Edric Ellis le 15 Mar 2013
Try:
mat_array = cell(3,50);
parfor i = 1:50
[a,b,c] = matrixgenerator(~);
mat_array(:,i) = {a,b,c};
end
In this way, you're performing a single indexing operation into mat_array, in a way that the PARFOR infrastructure can understand (i.e. the subscripts are a combination of the loop variable, and the literal ':').
  1 commentaire
Michael
Michael le 15 Mar 2013
This works very well, thank you. Out of curiosity, why have the brackets been switched to normal () ? Am I saving a 3x1 cell array {A,B,C} in a single cell of mat_array now?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by