Parfor indexing problem (Subscripted assignment dimension mismatch)

2 visualizaciones (últimos 30 días)
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 comentarios
Dan
Dan el 6 de Mayo de 2013
"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.
Dan
Dan el 6 de Mayo de 2013
Please help, someone? Anyone?

Iniciar sesión para comentar.

Respuesta aceptada

Edric Ellis
Edric Ellis el 7 de Mayo de 2013
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 comentario
Dan
Dan el 7 de Mayo de 2013
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by