在 MATLAB 中,我有一个变量proba
,parfor loop
如下所示:
parfor f = 1:N
proba = (1/M)*ones(1, M);
% rest of the code
end
pi_proba = proba;
MATLAB 表示:“临时变量 'proba' 在 PARFOR 循环之后使用,但它的值是不确定的”
我不明白如何纠正这个错误。我需要使用并行循环,并且需要proba
在循环之后。这个怎么做?
在 MATLAB 中,我有一个变量proba
,parfor loop
如下所示:
parfor f = 1:N
proba = (1/M)*ones(1, M);
% rest of the code
end
pi_proba = proba;
MATLAB 表示:“临时变量 'proba' 在 PARFOR 循环之后使用,但它的值是不确定的”
我不明白如何纠正这个错误。我需要使用并行循环,并且需要proba
在循环之后。这个怎么做?
使用parfor
类时根据这些类别进行分类。确保每个变量都与这些类别之一匹配。对于广播变量的非写入访问proba
将是最佳选择:
proba = (1/M)*ones(1, M);
parfor f = 1:N
% rest of the code
end
pi_proba = proba;
在循环内写入访问的情况下,切片变量是必要的:
proba=cell(1,N)
parfor f = 1:N
%now use proba{f} inside the loop
proba{f}=(1/M)*ones(1, M);
% rest of the code
end
%get proba from whatever iteration you want
pi_proba = proba{N};