Finding parfor baffling: Can anybody explain to me why this little bit of code works with for,but not with parfor?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
John Billingham
am 3 Sep. 2014
Kommentiert: John Billingham
am 4 Sep. 2014
function B = partest
A = 1:4; B = zeros(1,4);
parfor j=1:2
B([j j+2]) = A([j+2 j]);
end
The two bits of the loop access different bits of B, so there should be some way of doing this. My actual application involves large cell arrays, for which something similar holds for the function within the loop.
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
José-Luis
am 3 Sep. 2014
Bearbeitet: José-Luis
am 3 Sep. 2014
Looks like the interpreter is not smart enough to detect that there is no race condition in the case you present. You could go around that using two loops:
A = 1:4; B = zeros(1,4);
parfor j=1:2
B(j) = A(j+2);
end
parfor j=1:2
B(j+2) = A(j);
end
I assume the operations you actually perform are more complicated than that. Otherwise Matt J's solution is the way to go.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!