Error: Unable to classify a variable in the body of the parfor-loop

6 visualizaciones (últimos 30 días)
hello everyone,
I have the following code:
parfor i = 1:2^NJ-1
struct(i,:) = dec2bin(i,NJ);
end
M1 = 0;
for i = 1:NJ
R_M1 = 0;
for j = 1:2^NJ-1
temp = 0;
for p = 1:NJ
temp = temp + str2num(struct(j,p));
end
if temp == i
R_M1 = R_M1 + 1;
final(R_M1,:) = struct(j,NJ:-1:1);
end
M1 = R_M1
end
end
As it is, the code works fine but I want to change the loop after "M1=0" and change the first "For"(for i = 1:NJ) to "Parfor"(parfor i = 1:NJ) but when I do this and run the code I get the following error:
"Error: Unable to classify the variable 'final' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops"."
I think it may be due to the use of "struct(j,NJ:-1:1)" inside the "parfor" loop. Any help would be appreciated.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Ag. de 2021

You cannot do that. You are trying to emit a solution only under conditions, and you want that solution to be the next consecutive after any solution from a lower number iteration. But parfor does not do the iterations in strict increasing order so it cannot know where to write the solution.

You should instead be writing to final(i, :) and setting a variable indexed at i to indicate that final(i, :) is valid. Then after the parfor,

final = final(valid, :); 

to reduce down to the ones that were defined.

  5 comentarios
Walter Roberson
Walter Roberson el 11 de Ag. de 2021
That does not make sense to want that. The entire purpose of your
if temp == i
and your R_M1 logic is to sort the outputs into groups, the first group of which has 1 bit set, the second group has 2 bits set, the third group has 3 bits set, and so on.
Your new top code, the one you say shows the order of output you want, is the same as just doing
fliplr(dec2bin(1:1:2^NJ-1,NJ))
with no loop. There is no point in use parfor for that, or building struct or so on.
Jennifer Arellana
Jennifer Arellana el 11 de Ag. de 2021
you are right, I had done it with the for loop because it was my way of doing it but I will use this simpler way. Thank you for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) 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