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

13 views (last 30 days)
Hello everyone,
I have the following code:
orden = perms(1:n)
for i = 1:factorial(n)
for j = 1:n
pos(i,orden(i,j)) = j;
As it is, the code works fine but I want to change the first for loop to "Parfor", I get the following error:
"Error: Unable to classify the variable 'pos' 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 must be because "orden(i,j)" is used as an index of "pos" and "i" and "j" are indexes of "pos". Any help would be appreciated.

Accepted Answer

Edric Ellis
Edric Ellis on 10 Aug 2021
This doesn't work as written because the indexing form into pos doesn't meet the requirements for a "sliced" variable. The simplest way to fix this is to create a temporary vector in each iteration of the parfor loop, fill it using a for loop, and then assign a whole "slice" of pos. Like this:
n = 3;
orden = perms(1:n);
parfor i = 1:factorial(n)
% Make a temporary vector
tmp = zeros(1, n);
for j = 1:n
% Fill the temporary vector
tmp(orden(i,j)) = j;
% Assign a whole "slice" of pos
pos(i, :) = tmp;
3 2 1 2 3 1 3 1 2 2 1 3 1 3 2 1 2 3

More Answers (0)


Find more on Parallel for-Loops (parfor) 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