Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Edric Ellis
Edric Ellis il 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 Commento
Michael
Michael il 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?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by