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

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Edric Ellis
Edric Ellis el 10 de Ag. de 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

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