parfor error with 3D matrix
古いコメントを表示
Hello everyone,
I am using the parfor loop for the 3D matrix. When I am executing my parfor loop and getting this error "Error: The variable TEST1 in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview". I am replacing the zero value with nan in the loop. Any suggestion regarding this. Your help in this regard will be highly appreciated.
data = rand (20,10,10);
data1 = rand (20,10,10);
parfor ii=1:20
ii
for jj=1:10
jj
TEST1 (ii,jj,:) = data(ii,jj,:).*data1 (ii,jj,:);
TEST1 (TEST1==0) = nan;
end
end
採用された回答
その他の回答 (1 件)
sunny kant
2022 年 1 月 31 日
0 投票
3 件のコメント
Walter Roberson
2022 年 1 月 31 日
TEST1(TEST1==0) = nan;
inside the parfor loop would be an attempt to have the worker modify all of the variable TEST1, not just the part that is allocated to the loop. You should confine yourself to just the current worker's values
T2 = TEST1(ii,:,:);
T2(T2 == 0) = nan;
TEST1(ii,:,:) = T2;
Additionally
TEST1 (TEST1==0) = nan;
is an operation that Matlab already does in a fast, parallelized manner without explicit loops. You should reserve parfor only for things that cannot easily be replaced with vectorized expressions.
Matt J
2022 年 1 月 31 日
Thanks Walter for explaining me why my code is not working. As you suggested, I appllied for my code, and now it is working. Thanks for your suggestion on my code and clearing my doubts.
カテゴリ
ヘルプ センター および 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!