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

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

回答 (1 件)

Edric Ellis
Edric Ellis 2015 年 2 月 23 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

質問済み:

2015 年 2 月 19 日

回答済み:

2015 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by