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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Edric Ellis
Edric Ellis el 15 de Mzo. de 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 comentario
Michael
Michael el 15 de Mzo. de 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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by