フィルターのクリア

Finding parfor baffling: Can anybody explain to me why this little bit of code works with for,but not with parfor?

1 回表示 (過去 30 日間)
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.

採用された回答

Matt J
Matt J 2014 年 9 月 3 日
編集済み: Matt J 2014 年 9 月 3 日
A = reshape(1:4,2,2);
B = zeros(2);
parfor j=1:2
B(j,:) = A(j,end:-1:1);
end
  5 件のコメント
John Billingham
John Billingham 2014 年 9 月 3 日
OK. Thanks. I'll have a look at that tomorrow and see what I can do. I expect I'll have more questions. Really struggling to get my head around this. Sorry.
John Billingham
John Billingham 2014 年 9 月 4 日
Having given this some thought, I realize that your answer is great, but that I'm asking the wrong question, so I'm going to ask the right question instead.

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

その他の回答 (1 件)

José-Luis
José-Luis 2014 年 9 月 3 日
編集済み: José-Luis 2014 年 9 月 3 日
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.
  1 件のコメント
John Billingham
John Billingham 2014 年 9 月 3 日
What I really want to do is indeed more complicated than this, but thanks for your help.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by