# How can I perform a large number of curve fits more efficiently?

1 view (last 30 days)
Alex Magsam on 9 Nov 2016
Edited: John D'Errico on 9 Nov 2016
I have set of MRI images, and I am doing a pixel-by-pixel curve fit to calculate a certain coefficient I want. I then take that coefficient and assign it to a new matrix in its designated position. The images are 128x128, therefor there are 16,384 curve fits that need to be performed. Currently the process takes about an hour and half. I am looking for a way to reduce this time by a drastic amount. Any help is appreciated.
h = waitbar(0,'Wait');
%Curve fitting process
for j = 1:128
waitbar(j/128)
for i = 1:128
%T is a 5x1 vector (ind. variable)
%signal_array is an array with 128x128 5x1 vectors (dep. variable)
[fit,gof] = fit(T,signal_array{i,j},fittype);
coeff = coeffvalues(fit);
%If high correlation between values, plot T value directly
if gof.rsquare > .8
t2map(i,j) = 1/coeff(2);
%If medium correlation between values, plot T value located directly to
%the left of that pixel
elseif (gof.rsquare <= .8) && (gof.rsquare >=.5)
t2map(i,j) = t2map(i,(j-1));
%If there is low correlation, assign the value to 0
else
t2map(i,j) = 0;
end
clear fit coeff gof
end
end

John D'Errico on 9 Nov 2016
Edited: John D'Errico on 9 Nov 2016
One way yo do it is to use a batched nonlinear regression.
Because of the nature of the partitioned least squares, you also gain in terms of speed, because it reduces the effective size of the problem. But you will need to learn how to split the variables into conditionally linear versus truly nonlinear sets. I can't help you there, because you don't tell us that model you use.
I don't know of a better solution. It has been a while, but I recall throughput speeds of as much as 250 to 1 compared to a simple loop when I originally developed the idea. That will depend on your problem of course. I see in my comments that a 3 variable example showed a 13-1 speed boost.
You will need to compute the various statistics yourself. But that should be trivial in most cases.
You will also need the optimization toolbox, since I don't believe the curve fitting toolbox has the necessary sparse Jacobian matrix capabilities that I use.