1
votes

I'm trying to run this code in Matlab

a = ones(4,4);

b=[1,0,0,1;0,0,0,1;0,1,0,0;0,0,0,0];
b(:,:,2)=[0,1,1,0;1,1,1,0;1,0,1,1;1,1,1,1];

parfor i = 1:size(b,3)
    c = b(:,:,i)
    a(c) = i;

end

but get the error:

Error: The variable a in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
1

1 Answers

3
votes

There are restrictions in how you can write into arrays inside the body of a parfor loop. In general, you will need to use sliced arrays.

The reason behind this issue is that Matlab needs to prevent that different worksers access the same data, leading to unpredictable results (as the timely order in which the parfor loops through i is not detemined).

So, although in your example the workers don't operate on the same entries of a, due to the way how you index a (with an array of logicals), it is currently not possible for Matlab to decide if this is the case or not (in other words, Matlab cannot classify a).

Edit: For completeness I add some code that is equivalent to your example, although I assume that your actual problem involves more complicated logical indexing?

a = ones(4,4,4);

parfor i = 1:size(a,1)
    a(i, :, :) = zeros(4, 4) + i;  % this is sliced indexing
end

Edit: As the OP example was modified, the above code is not equivalent to the example anymore.