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

2 views (last 30 days)
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

Accepted Answer

Edric Ellis
Edric Ellis on 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 Comment
Michael
Michael on 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?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!