フィルターのクリア

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

3 ビュー (過去 30 日間)
Jennifer Arellana
Jennifer Arellana 2021 年 8 月 9 日
回答済み: Edric Ellis 2021 年 8 月 10 日
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.

採用された回答

Edric Ellis
Edric Ellis 2021 年 8 月 10 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by