Error: Unable to classify a variable in the body of the parfor - loop
3 views (last 30 days)
Show older comments
Jennifer Arellana
on 9 Aug 2021
Answered: Edric Ellis
on 10 Aug 2021
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.
0 Comments
Accepted Answer
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;
end
% Assign a whole "slice" of pos
pos(i, :) = tmp;
end
disp(pos)
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!