Nested for-Loops in parfor

1 回表示 (過去 30 日間)
LD
LD 2022 年 3 月 22 日
コメント済み: LD 2022 年 4 月 10 日
I try to put a for-loop in a parfor-loop. However, the following code is invalid.
parfor i=1:10
for j=1:10
a(j)=1;
end
end
The only way I know to fix that is
parfor i=1:10
for j=1:10
a(i,j)=1;
end
end
But I think it is inefficient when the size of "a" is large (I don't need "a" at the end of this program) . Is there a better way to implement this nested for-loop?
  1 件のコメント
Edric Ellis
Edric Ellis 2022 年 3 月 23 日
@Walter Roberson has provided you with a way to fix the problem. Just to explain the problem a little bit more - parfor insists on being able to prove that the iterations of your parfor loop are not (trying to be) order-dependent. In your original code, it appears as though the value of a as assigned when i==1 is still available on the next iteration when i==2. Walter's fix assigns a value to the whole of a, and this makes parfor satisfied that you cannot possibly access the old values of a in subsequent iterations.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 22 日
parfor i=1:10
a = zeros(10, 1);
for j=1:10
a(j)=1;
end
end
  1 件のコメント
LD
LD 2022 年 4 月 10 日
thanks

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by