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

3 Ansichten (letzte 30 Tage)
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;
end
end
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.

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 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;
end
% Assign a whole "slice" of pos
pos(i, :) = tmp;
end
disp(pos)
3 2 1 2 3 1 3 1 2 2 1 3 1 3 2 1 2 3

Weitere Antworten (0)

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by