how do I slice a variable? parfor loop

Hello guys,
Matlab is showing me an error that involves a parfor loop. Below you can see the structure of the code and the error. It is the first time I'm using the "parfor" command.
Any ideas on how to solve this problem? Thankserror.JPG
parfor counter3=1:n3
for counter2=1:n2
for counter1=1:n1
for i=1:N
for k=1:m-1
quantity(k+1,1)=something;
end
Quantity_av(i,1)=sum(quantity)/m;
end
QT=horzcat(QT,Quantity_av);
end
end
end

답변 (1개)

Edric Ellis
Edric Ellis 2019년 1월 11일

2 개 추천

The problem here is that the parfor machinery cannot tell that you are completely overwriting quantity and Quantity_av in your inner loops. (In fact, you might not be, it's impossible to tell from the code you posted). This leads the parfor machinery to conclude that those variables are being used in such a way as to cause a dependence between the order of iterations in the loop. (If you don't full clear out e.g. quantity, then on the second iteration of the parfor loop, the values computed in the first iteration are still present, and so might influence the result of the second iteration - that's not allowed).
Presuming you did intend to completely overwrite quantity and Quantity_av, then the fix is simple: initialise those variables like so:
parfor counter3=1:n3
for counter2=1:n2
for counter1=1:n1
Quantity_av = zeros(n1, 1);
for i=1:N
quantity = zeros(m, 1);
for k=1:m-1
quantity(k+1,1)=something;
end
Quantity_av(i,1)=sum(quantity)/m;
end
QT=horzcat(QT,Quantity_av);
end
end
end
This initialisation causes the parfor machinery to conclude that quantity and Quantity_av are temporary variables, and thus the loop can proceed.

댓글 수: 7

Juan Nunez
Juan Nunez 2019년 1월 11일
Hello Edric,
Thank you for your answer. The problem is I need to keep all Quantity_av values. Is there a way to do this?
Edric Ellis
Edric Ellis 2019년 1월 14일
The variable QT will store all the concatenated values of Quantity_av - isn't that what you want?
Stephen23
Stephen23 2019년 1월 14일
Juan Nunez's "Answer" moved here:
Apparently, QT is only storing the last value. The rest of the stored values is "0"
I don't see that. I slightly modified the example to make it executable, and QT accumulates the result:
n1 = 3;
n2 = 4;
n3 = 5;
N = 6;
m = 7;
QT = [];
parfor counter3=1:n3
for counter2=1:n2
for counter1=1:n1
Quantity_av = zeros(n1, 1);
for i=1:N
quantity = zeros(m, 1);
for k=1:m-1
quantity(k+1,1)=rand();
end
Quantity_av(i,1)=sum(quantity)/m;
end
QT=horzcat(QT,Quantity_av);
end
end
end
madhan ravi
madhan ravi 2019년 1월 15일
Juan Nunez's answer moved here for consistency:
Thanks Edric. Your last code was very helpful. I solved the issue.
madhan ravi
madhan ravi 2019년 1월 15일
편집: madhan ravi 2019년 1월 15일
@Juan Nunez stop adding answers to make a comment , instead make a comment to the answer which you are responding to.
Juan Nunez
Juan Nunez 2019년 1월 15일
편집: madhan ravi 2019년 1월 15일
I'm sorry Madhan, my mistake.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2015b

질문:

2019년 1월 10일

편집:

2019년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by