How can I convert this loop from a 'for' loop to a 'parfor' loop?
Show older comments
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
on 23 Feb 2015
0 votes
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!