Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Parfor error using variables.

조회 수: 2 (최근 30일)
Priya
Priya 2013년 8월 10일
마감: MATLAB Answer Bot 2021년 8월 20일
a = 0; aa_array = cell(zeros)
parfor i = 1:20
if 1 == 1
a = a + 1;
aa_array(a,1) = {'done'} ;
end
This code is an simpliefied mimic of the code which I am executing. It is running fine with for loop.
However it gives error "Error, parfor cannot be used with the way variables a and aa_array are being used".
Whats the problem in this ?

답변 (1개)

Walter Roberson
Walter Roberson 2013년 8월 10일
Although you are setting all the elements to the same value in this particular case, it would only take a trivial modification of your code to arrive at a situation in which the values were not all the same. Then because parfor is allowed to execute the iterations in any order, relying on the sequential increment of "a" is not going to work: for example iterations i=1, i=5, and i=14 might all be running simultaneously in the "first round" at the time that a is 0, so depending on exact timing and internal implementation, you could end up writing into aa_array(1,1) three times or aa_array(3,1) three times, or (1,1) twice and (2,1) once, or any combination. Likewise, "a" could end up as 1 or 2 or 3 depending on how the three iterations "race" each other.
aa_array = cell(20,1);
parfor i = 1 : 20
if 1 == 1
aa_array(i,1) = {'done'};
end
end
  댓글 수: 1
Priya
Priya 2013년 8월 11일
Hi
Thanks for your reply. But I cant use:
aa_array(i,1) = {'done'};
the reason is that if the condition is not met, it will create empty cells corresponding to those i values in the aa_array cell array.
So i have to use another variable 'a' to avoid such blank cells.
my if condition is different. To make things simple to understand I used an arbitary condition.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by