how do I slice a variable? parfor loop
1 次查看(过去 30 天)
显示 更早的评论
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? Thanks
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
0 个评论
回答(1 个)
Edric Ellis
2019-1-11
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!