How to execute for-loop iterations in parallel
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Benjamin Thompson
2022-2-2
Can you post a working example of sequential MATLAB code that implements your algorithm? You can always set all of X to zeros before the for loop to simplify part of your algorithm. parfor usually cannot write to different locations in a matrix in parallel. You may be able to write the result to an output vector in parfor, then redim the output vector to mxnxp.
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!