How to execute for-loop iterations in parallel

1 ビュー (過去 30 日間)
Vinit Nagda
Vinit Nagda 2022 年 2 月 2 日
コメント済み: Ive J 2022 年 2 月 2 日
I want to implement parfor to speed up the calculation but I am facing errors.
How can I implement parfor for the following for loop:
I have a 3D matrix X (DIM: mxnxp)
trues=find(X);
for i=1:size(trues,1)
[x,y,z]=ind2sub([m n p],trues(i,1))
if (condition true)
X(x,y,z)=0;
end
end
Is there any other way I can speed up and optimize execution?
Thank you.
  2 件のコメント
Vinit Nagda
Vinit Nagda 2022 年 2 月 2 日
m=300;n=300;p=50;
X=ones(m,n,p);
trues=find(X);
for i=1:size(trues,1)
[x,y,z]=ind2sub([m n p],trues(i,1));
if (x>y+z)
X(x,y,z)=0;
end
end
@Benjamin Thompson Here is the working example. Although the if condition is different in real problem, but algorithm remains same.

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

回答 (1 件)

Ive J
Ive J 2022 年 2 月 2 日
編集済み: Ive J 2022 年 2 月 2 日
You don't need even a loop (parfor aside) for this (and I guess you don't even need ind2sub depending on your true purpose here):
m=300; n=300; p=50;
X = ones(m, n, p);
trues = find(X > 0);
[x, y, z] = ind2sub([m n p], trues);
idx = x > y + z;
X(trues(idx)) = 0;
  2 件のコメント
Ive J
Ive J 2022 年 2 月 2 日
Maybe you can explain the probelm itself then :)

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

カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by