parfor with Matlab “the variable __ in a parfor cannot be classified”

微笑、不失礼 提交于 2019-12-02 07:40:17

@Andras Deak put this in a comment, but the fix here is really simple - all you need to do is use a form of indexing for the second subscript to coef that is allowed by parfor. In this case, you need to do:

parfor i = ...
    coef(i, :) = ...;
end
Adriaan

You are trying to fill an array before the amount of elements to be filled is known. What that means is that your loop has a different length of current_coef, which cannot be determined before the loop is run. You need to define everything before the parallel loop executes, because the order of execution is not predetermined.

The way to solve this is given by @AndrasDeak in the comments on this post. (I did not know the function, or that it always output the same number of elements, hence I could only give an explanation as to why this code does not work, but not the solution)

In general it is best to first optimise your code in terms of speed, then try and go parallel. Even without regression. Parallel processing is not some magic want you wave over sloppy code to magically make it execute faster, it is a highly optimised tool which works best on optimised codes.

For a better overview of parfor see Saving time and memory using parfor in Matlab?

Note you called this piece of code a "regression" in one of your earlier posts today, which it is not. If it were regression, however, parfor would not work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!