So I am trying to call this function using a parfor
(basically curve fitting using Fourier series through a vector in a parfor
loop):
f
@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
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.