Search code examples
matlabloopsvectorizationcurve-fittingcell-array

How to evaluate matlab fit objects in a cell array without looping?


I have an array of fit objects and I need to evaluate each of them with several values. Because there are over thousand of those fit objects I find it very slow to loop over them and evaluate them with the values. So is there a way to use some kind of vectorized solution to this?

For example I can evaluate a single fit object by

fitArray{1,1}(400)

but what I would like to do is to evaluate multiple fit objects at a time in a way something like this:

fitArray{1:1000}(400)

The looping in Matlab is always very slow and in this case it's really slow as I need to evaluate each of those fits with multiple values.

So is there a way to do that without looping?


Solution

  • I found the answer myself. It was quite simple after all. I achieved the result I wanted by doing this:

    vals = repmat({values}, size(fitArray));
    evals = cellfun(@feval, fitArray, vals);
    

    This evaluates each fit object in the cell array with the value in the corresponding row in the vals array. So the result is that the evals array has only the results of each fit object.