如何在MATLAB中使用PARFOR循环外的变量?

时间:2014-06-17 21:41:40

标签: matlab parallel-processing parfor

在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。怎么做?

1 个答案:

答案 0 :(得分:4)

使用parfor时,会根据these categories.对类进行分类。确保每个变量都符合其中一个类别。对于proba的非写入访问,广播变量将是最佳选择:

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

如果在循环中写入访问权限,则切片变量是nessecary:

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};
相关问题