reading and writing a cell array in a parfor loop

36 visualizaciones (últimos 30 días)
Moritz H
Moritz H el 9 de Ag. de 2016
Comentada: Moritz H el 10 de Ag. de 2016
Hey!
I just came across this and I don't quite understand it. Why is MATLAB complaining about dependencies in different loop iterations when I do something like this:
%minimum working example:
a = cell(100,2);
%fill first row with values;
a(:,1) = {ones(1,1)};
%c = a;
parfor i=1:size(a,1)
b = a{i,1}(1) + 2;
a{i,2} = b;
end
Why can't I read the row I am working on prior to writing in it? I am using the same i every time... Do I really have to copy the whole array to c and read from that to run this parallel?
Thank you!

Respuesta aceptada

Edric Ellis
Edric Ellis el 10 de Ag. de 2016
One of the restrictions of parfor is that for a sliced variable, you need to use precisely the same form of indexing each time you use that variable. This is described in the doc. To fix this, you need to make a slight change to ensure that you always index into a using a fixed index listing, like so:
parfor i=1:size(a,1)
arow = a(i,:);
b = arow{1}(1) + 2;
arow{2} = b;
a(i,:) = arow;
end
  1 comentario
Moritz H
Moritz H el 10 de Ag. de 2016
nice, thank you! I didn't know I had to use the exact same indexing. I thought, operating on the same row would be enough...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by