Parfor indexing problem (Subscripted assignment dimension mismatch)

1 次查看(过去 30 天)
I have set up a parfor loop and am receiving the following indexing problem:
Error using parallel_function (line 598)
Subscripted assignment dimension mismatch.
Error in Abel_Shell (line 17)
parfor i = 1:len
This is my code:
len = length(phase(1,:));
parfor i = 1:len
line = phase(i,:);
if max(line) > 1e-2
cen = round(trapz(xaxis.*line)/trapz(line));
lhs = fliplr(line(1:cen));
rhs = line(cen+1:length(line));
denlhs = Abel_DanSteve(lhs,pixel);
denrhs = Abel_DanSteve(rhs,pixel);
density(i,:) = [fliplr(denlhs), denrhs];
else
density(i,:) = 0;
end
end
I have confirmed that len = 3000 at the start of the loop as expected. What could this be?
Thanks, Dan
  3 个评论
Dan
Dan 2013-5-6
"parallel_function" is Matlab's function for implementing "parfor" and it won't let me look at the code, so I don't know what line 598 is. The code you see here is part of "Abel_Shell". Line 17 is the beginning of the parfor statement : "parfor i = 1:len".
The error comes before the loop even starts, so I don't know what it would have to do with cen. However to answer your question, when I run a simple for loop, everything works fine and cen is always greater than 0.
As an add on, I tried to hard code the line to read : "parfor i = 1:3000", and it gives me the same error. I tried different numbers, for some reason it works for "parfor i = 1900:2000", but almost all other ranges give me the same error, including "parfor i = 1:100". I am thoroughly confused.

请先登录,再进行评论。

采纳的回答

Edric Ellis
Edric Ellis 2013-5-7
Firstly, I think your PARFOR loop bounds are suspicious - you calculate 'len' as the number of columns in 'phase', but then use that as the upper bound on the number of rows you're going to access.
I suspect the actual problem is that you haven't pre-allocated 'density'. Normally with PARFOR, you do not need to do this; however in your case I suspect that the 'else' clause is being hit, and you might be hitting an ordering where MATLAB doesn't know how large to make 'density'. The other way to fix this would be to always assign into 'density' using the correct size zeros. I would perhaps modify your code like this:
[nRows, nCols] = size(phase);
parfor i = 1:nRows
line = phase(i, :);
if ...
...
else
density(i, :) = zeros(1, nCols);
end
end
  1 个评论
Dan
Dan 2013-5-7
Thanks Edric, my code is now working. Part of the problem here was that I wasn't copying the whole code to keep the question more focused. 'density' was preallocated, and the mixup between columns and rows was me being lazy because every data matrix that comes in is square. I changed the indexing (parfor line above) to how you said, though I don't think that made the difference. I also changed the else statement to how you wrote it. I'm guessing that was what made it work. Either way, thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by