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

14 views (last 30 days)
hello everyone,
I have the following code:
parfor i = 1:2^NJ-1
struct(i,:) = dec2bin(i,NJ);
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));
if temp == i
R_M1 = R_M1 + 1;
final(R_M1,:) = struct(j,NJ:-1:1);
M1 = R_M1
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.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Aug 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.

Jennifer Arellana
Jennifer Arellana on 11 Aug 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!

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by