Faster method for polyfit along 3rd dimension of a large 3D matrix ?

8 views (last 30 days)
I tried to understand the answer but I cant seem to understand the mathematics behind it, I dont know why does it give the right answer.
Also, in my program I do exponential fit along the 3rd dimension of a large 3d matrix by two for loops.
My code:
dataCube = rand(480,640,500);
x = rand(500,1);
sizeCube = size(dataCube);
exCube = zeros(sizeCube);
for ii = 1:sizeCube(1);
for iii = 1:sizeCube(2);
s(1,:) = dataCube(ii,iii,:);
signal = s';
a = fit(x,signal,'exp2')
y = feval(a,x);
exCube(ii,iii,:) = y';
end
end
This code takes several hours to finish running.
Is there a fast method for exp fit along 3d dimesnion for large 3d data matrix ?
Any help would be greatly appreciated.

Answers (1)

Matt J
Matt J on 12 Sep 2014
Edited: Matt J on 12 Sep 2014
Because a polynomial has a linear dependence on the unknown coefficients, the solution for a polynomial fit is a closed-form linear function of the input data, and therefore can be expressed as a matrix multiplication. You can't accelerate the fitting for exp2 the same way as for a polynomial, because the least squares fit for exp2 is neither linear nor closed-form as a function of the input signal.
Nevertheless, there are tricks you can use, depending on your dataCube. For example, if neighboring signals in your dataCube are always very similar to each other, you can initialize the fit of one signal in the set with the solution from the previous signal. This reduces the number of required iterations in the fit.
  4 Comments
Lucia
Lucia on 12 Sep 2014
Thank you very much, I looked at the file but didn't quite understood it (lots of stuff there).
Can you please give me a coded example of how to use it in my code ?
Thanks in advance
Matt J
Matt J on 13 Sep 2014
Edited: Matt J on 13 Sep 2014
Here is an example exp2 fitting using FMINSPLEAS,
x = linspace(0,3,100);
y = 4*exp(-3*x) - 5*exp(-2*x);
funlist = {@(cd,x) exp(cd(1)*x),...
@(cd,x) exp((cd(1)+cd(2))*x)};
cd0 = [-1,-4]; %initial guess
options = optimset('disp','iter','TolX',1e-20,'TolFun',1e-20);
[cd,ab] = fminspleas(funlist,cd0,x,y,[],[],[],options);
a=ab(1),
b=ab(2),
c=cd(1),
d=cd(1)+cd(2),

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!