How can I convert this loop from a 'for' loop to a 'parfor' loop?

1 view (last 30 days)
function M=getMatrix(P,T)
n=length(P);
[ne,~]=size(T);
M=sparse(n,n);
for i=1:ne
k=T(i,:);
Me=subMatrix(P(k,:));
M(k,k) = M(k,k) + Me;
end
return

Answers (1)

Edric Ellis
Edric Ellis on 23 Feb 2015
I suspect this is not likely to be possible directly since you are effectively updating arbitrary elements of M on each iteration of the loop. PARFOR loops can only operate when each loop iteration accesses separate elements of the output variables (this is known as "slicing").
You might get some benefit by performing the underlying calculations in a PARFOR loop and then having a separate FOR loop to update M afterwards - providing the calculation inside subMatrix is expensive enough.

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!