How can I change this code to be able to use parfor?

1 回表示 (過去 30 日間)
MRC
MRC 2013 年 10 月 31 日
コメント済み: Matt J 2013 年 11 月 1 日
A=randn(5,1);
parfor i=1:4
B=randn(3,1);
for t=1:size(B,1)
for w=1:size(A,1)
if B(t)==A(w)
A(w)=A(w)+1;
end
end
end
end

回答 (1 件)

Matt J
Matt J 2013 年 10 月 31 日
編集済み: Matt J 2013 年 10 月 31 日
A=randn(5,1);
C=cell(1,4);
parfor i=1:4
B=randn(1,3);
C{i}=sum(bsxfun(@eq,A,B),2);
end
A=A+sum([C{:}],2);
  1 件のコメント
Matt J
Matt J 2013 年 11 月 1 日
Better:
A=randn(5,1);
C=0;
parfor i=1:4
B=randn(1,3);
C=C+sum(bsxfun(@eq,A,B),2);
end
A=A+C;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by