parfor error with 3D matrix

조회 수: 1 (최근 30일)
sunny kant
sunny kant 2022년 1월 30일
댓글: Matt J 2022년 1월 31일
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

채택된 답변

Matt J
Matt J 2022년 1월 30일
Move that line outside the loop.
parfor ii=1:20
ii
for jj=1:10
jj
TEST1 (ii,jj,:) = data(ii,jj,:).*data1 (ii,jj,:);
end
end
TEST1 (TEST1==0) = nan;

추가 답변 (1개)

sunny kant
sunny kant 2022년 1월 31일
Thanks you Matt for your suggestion on the code. However, I am applied the same thing, but when I am applying TES1 (TEST1==0) = nan inside the parfor loop in the code, it gives error. Can you suggest me that why it is happing.
  댓글 수: 3
Matt J
Matt J 2022년 1월 31일
편집: Matt J 2022년 1월 31일
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
Matt J 2022년 1월 31일
sunny kant's comment moved here
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by