Error: The variable in a parfor cannot be classified.
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to convert my code over to run with parfor, since as it is it takes a long time to run on its own. However I keep getting this error. I have search around on the website and have read people with similar problems, but none of those answers seem to fix my problem. My code is as follows. dArea is the variable that seems to be giving me the issue. It is preallocated.
parfor i=1:500
data = regionprops(cc, 'Area');
Area=[data.Area];
Number(i)=length(Area);
for j=1:length(Area)
dArea(i,j)=Area(1,j);
end
end
2 comentarios
Walter Roberson
el 10 de Jun. de 2015
As an experiment, what happens with
parfor i = 1 : 500
data = regionprops(cc, 'Area');
Area = [data.Area];
len = length(Area);
Number(i) = len;
dArea(i,1:len) = Area(1:len);
end
Edric Ellis
el 11 de Jun. de 2015
Performing a single assignment like this is definitely preferable. Unfortunately, in the form as you have it, dArea still cannot be classified because it doesn't follow the "slicing" rules - instead of indexing with 1:len, the second subscript must be simply :. There's more about the slicing rules here: http://www.mathworks.com/help/distcomp/sliced-variables.html
Respuestas (1)
Edric Ellis
el 11 de Jun. de 2015
An equivalent problem is the following:
parfor i=1:5
data = rand(5, 1);
for j = 1:numel(data)
out(i, j) = data(j);
end
end
The problem is the loop bounds on the inner for loop. As described in the documentation, a nested for loop must use constant bounds. So, you could fix the example above like so:
n = 5;
parfor i=1:n
data = rand(n, 1);
for j = 1:n % loop bound is now a known constant value
out(i, j) = data(j);
end
end
However, as Walter suggests, it's better still to do this:
n = 5;
parfor i=1:n
data = rand(n, 1);
out(i, :) = data;
end
to avoid having a simple assignment loop.
3 comentarios
Walter Roberson
el 11 de Jun. de 2015
The tArea I showed creates a temporary vector of the maximum length, of all 0, write the data into it, and then write the vector as a column.
The code you have as a loop does not write into some locations so the result depends on how you initialize it which makes it both input and output. The temporary vector of 0 gives definite value to all locations and makes it output only.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!