Fastest (parallelized, maybe) way to run exponential fit function in a big matrix of data
Show older comments
Dear experts,
I am trying to ensure the most optimized syntax to run a part of my code. In summary, I need to perform an exponential fit of a series with 6 points, but ~2 million times (independent samples). In other words, I have a reshaped matrix 6x2,000,000.
This is my code right now (each loop iteration takes 0.02 seconds what makes this process prohibitive):
Xax = [30;60;90;120;150]; % The constant X series.
f = @(b,Xax) b(1).*exp(b(2).*Xax); % Exponential model
% LoopMap == my 6x2,000,000 matrix
for k = 1:size(LoopMap,2) %parfor? Any GPU model (if this is and optimal example)
bet = fminsearch(@(b) norm(LoopMap(:,k) - f(b,Xax)),[0;0]);
CoefAtmp(1,k) = abs(1/bet(2));
end
My PC is a standard machine (8th gen Core i7, 32Gb ram) with a not exceptional graphic card.
Thank you all in advance.
1 Comment
Matt J
on 29 Sep 2021
Do you have the Parallel Computing Toolbox? Is parfor an option?
Accepted Answer
More Answers (1)
If it's acceptable to you, a log-linear least squares fit can be done very fast and without loops
A=Xax.^[0,1];
Bet=A\log(LoopMap);
CoefAtmp=1./abs(Bet(2,:));
3 Comments
Brunno Machado de Campos
on 29 Sep 2021
Both methods are exponential fits.
Do you mean you must have a linear least squares objective? If so, intiailizing fminsearch with the log-linear solution will probably speed things up.
However, you should first check that the linear and the loglinear methods give significantly different results over a small but representative subset of your LoopMap(:,k). If not, you can use that to defend the loglinear method to the academic community.
Brunno Machado de Campos
on 29 Sep 2021
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!