"parfor中的变量无法分类." Matlab中的错误 [英] "the variable in a parfor cannot be classified." error in Matlab
问题描述
我正在尝试使用parfor实现一个非常简单的程序,但是出现一些错误.我看到几乎所有的SO问题都有可能重复,但是没有一个与我的问题情况相似.我得到的错误是:
I am trying to implement a very simple program with parfor but I get some errors. I saw nearly all of the SO questions for a possible duplication but non of them was similar to my question situation. The error I get is :
错误:变量 log_likelihood_II_with_entropy 在parfor中不能是 分类.
Error: The variable log_likelihood_II_with_entropy in a parfor cannot be classified.
我的代码写在下面:
em_iterations=10;
users=5;
log_likelihood_II_with_entropy=zeros(users,em_iterations);
parfor u = 1:1:users
for current_iter=1:1:em_iterations
log_likelihood_II_with_entropy(u,current_iter)=rand();
end
end
推荐答案
由于log_likelihood_II_with_entropy
依赖于parfor
索引(u
)和内部索引"(current_iter
),因此无法对其进行分类.每个parfor
迭代均独立于其他迭代,并且不会按顺序执行(即u
不一定会按1,2,3,4,...,
Since log_likelihood_II_with_entropy
relies on both the parfor
index (u
) and an "inside index" (current_iter
) it cannot be classified. Every parfor
iteration is independent from the others and they are not executed in order (that is, u
will not necessarily go from 1 to users
in order 1,2,3,4,...,users
).
我的建议是让单个parfor
迭代(工作人员)构建整个log_likelihood_II_with_entropy
行.
My suggestion is to let the single parfor
iteration (worker) build an entire row of log_likelihood_II_with_entropy
.
parfor u=1:users
single_row=zeros(1,em_iterations);
for current_iter=1:1:em_iterations
single_row(current_iter)=rand();
end
log_likelihood_II_with_entropy(u,:)=single_row;
end
以这种方式,每个parfor
任务(parfor
主体本身)将预分配并评估单个行,而不管u
的值是多少.然后它将替换/连接log_likelihood_II_with_entropy
矩阵中的该值.
In this manner every parfor
task (the parfor
body itself) will preallocate and evaluate a single row, no matter what the u
value is. And then it will replace/concatenate such value in the log_likelihood_II_with_entropy
matrix.
这篇关于"parfor中的变量无法分类." Matlab中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!