在MATLAB中,我有一个变量proba和一个parfor loop如下所示:

parfor f = 1:N
    proba      = (1/M)*ones(1, M);
    % rest of the code
end
pi_proba = proba;

Matlab称:“临时变量‘PROPA’是在PAROF循环之后使用的,但其值是不确定的”
我不明白如何改正这个错误我需要使用一个并行循环,循环之后我需要proba怎么做?

最佳答案

当使用parfor时,根据these categories.对类进行分类,确保每个变量都与这些类别中的一个匹配对于proba的非写访问,最好选择广播变量:

proba      = (1/M)*ones(1, M);
parfor f = 1:N
    % rest of the code
end
pi_proba = proba;

在循环内写入访问时,切片变量为nesescary:
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};

08-27 12:36