get matrix in parfor

조회 수: 4 (최근 30일)
vu ngothanh
vu ngothanh 2016년 4월 4일
답변: Edric Ellis 2016년 4월 12일
Dear all,
I have this code
result=[]
for m=1:10
for k=1:100
for c=1:50
a=m+k*c;
b=m*k-c;
result=[result;a b];
end
end
end
I try to convert from for to parfor like this
result=[]
for m=1:10
parfor k=1:100
for c=1:50
a=m+k*c;
b=m*k-c;
result=[result;a b];
end
end
end
But there is a problem, "the variable 'result' used a value outside of the loop". I has searched from the site, but i'm not understand about the solution.
How can I fix it.

답변 (1개)

Edric Ellis
Edric Ellis 2016년 4월 12일
You can't update the parfor reduction variable result inside the inner loop, you need to update it directly in the body of the parfor loop. You can work around this by making a new temporary reduction variable for each iteration of the parfor loop, and then update result once that's complete, like so:
result=[];
for m=1:10
parfor k=1:100
tmp = [];
for c=1:50
a=m+k*c;
b=m*k-c;
tmp=[tmp;a b];
end
result = [result; tmp];
end
end

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by