Indexing error using the 'parfor' command

10 次查看(过去 30 天)
I am new to parallizing code. I am trying to implement a simple parfor. Each iteration of the loop is independent of all others, which I thought was the only consideration in using parfor. But Matlab is giving me an error saying the the () line has invalid indexing for the density variable. What's interesting is it doesn't have a problem with the line just above it. I tried reading about "Sliced Variables" at Matlab's suggestion, but it didn't get me anywhere. Can anyone give me a pointer here?
Thanks, Dan
PS - I realize there's an error in those lines where I'm overwriting the "cen" index, but that's an independent issue.
len = length(phase);
parfor i = 1:length(data.phasemap)
line = phase(i,:);
if max(line) > 1e-2
cen = round(trapz(xaxis.*line)/trapz(line));
lhs = fliplr(line(1:cen));
rhs = line(cen:length(line));
denlhs = Abel_DanSteve(lhs,data.pixel);
denrhs = Abel_DanSteve(rhs,data.pixel);
density(i,1:cen) = fliplr(denlhs);
density(i,cen:len) = denrhs; %**
else
data.density(i,:) = 0;
end
end

采纳的回答

Edric Ellis
Edric Ellis 2013-5-4
When slicing an output variable, PARFOR requires that you always use the same indexing expression. It should work to do:
density(i, :) = [fliplr(denlhs), denrhs];
Or similar. This way, you're always assigning into density the same way.
  1 个评论
Dan
Dan 2013-5-6
编辑:Dan 2013-5-6
Thank you Edric. That seemed to fix the problem with output variable indexing. Now, there is a new problem with the loop index. Here is the error:
--------------------------------------------------
Error using parallel_function (line 598)
Subscripted assignment dimension mismatch.
Error in Abel_Shell (line 17)
parfor i = 1:len
--------------------------------------------------
I have changed a couple lines to make things more simple, but it didn't change the problem.
--------------------
len = length(phase(1,:));
parfor i = 1:len
...
---------------------
I have confirmed that i = 3000 when the parfor loop starts. What could this be?
I guess I should post this as a new question. Look for parfor indexing problem.

请先登录,再进行评论。

更多回答(0 个)

类别

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