MATLAB Answers

Speed up (vectorize?) nlinfit

7 views (last 30 days)
Ioannis on 13 Sep 2013
Hello all.
I have been using nlinfit to extract two parameters (p1 and p2) from a simple exponential equation on a pixel basis for medical images. The dependent variable values are stored in Y (e.g. 53248x5 for an image of 208x256 pixels and 5 variable values). The 5 independent variables values are stored in X (e.g. 1x5 for 5 values). I am doing the fitting using the following bit of code:
equation=@(p,X) p(1)*exp(-X*p(2));
parfor i=1:d1*d2
if sum(Y(i,:))>60;
B(i,:)=nlinfit(X,Y(i,:),equation,[Y(i,1) 0.001],options);
The threshold of 60 is to make sure that fitting only takes place in pixels with useful signal (above noise level). The issue with the code above is that it takes almost 25sec to complete the pixel-based fitting for an image of 208x256 pixels and this is too long (even with parfor and 4 workers). I have tried various ways to vectorize the code above and speed things up, but with no success.
I would really appreciate any ideas.
Regards, Ioannis.
Ioannis on 18 Sep 2013
Hello Matt and thanks for your comments. Yes, I have pre-allocated B and by pre-eliminating Ys (Xs are always the same), I saved 4-5 sec, which is not the acceleration factor I was hoping for... (also, regridding the data to get a meaningful image gets a bit tricky)

Sign in to comment.

Answers (3)

Matt J
Matt J on 13 Sep 2013
Edited: Matt J on 13 Sep 2013
If the fit can be done with 5 data points only, I assume the data errors are very small, in which case, you might get a pretty accurate fit just by doing a linear fit to the log of your model
logY = log(p(1)) - X*p(2)
This can be solved for a=log(p(1)) and b=-p(2) using any of MATLAB's solvers, and of course it can be further vectorized across all Y(i,:),
Xcell=[ones(size(X)), X].';
Xcell= num2cell(reshape(Xcell, 5,2,[]),[1,2] );
If nothing else, this might give you a better beta0 than [Y(i,1) 0.001] and that, of course, could speed things up.
Lennart on 19 Sep 2013
Matt thats a very helpful suggestion, thanks.

Sign in to comment.

Lennart on 19 Sep 2013
Hi Loannis, I have found this FEX file. It seems to do what you want. What was your final implementation? I am trying to solve exactly the same problem. My loops take hours on my data sets, now trying the FEX file.
  1 Comment
Matt J
Matt J on 19 Sep 2013
I don't have NLINFIT and can't test my theories, but the FEX file probably was not intended for combining such a large number (~1e4) of model functions as in Ioannis's case. In particular, it does not vectorize the function evaluations in an optimal way. It just takes the N original anonymous functions specifying the models and and nests them inside a larger anonymous function.
If you are going to try wrapping the N problems together into a single problem, it would probably be better to do so manually, by writing your own N-dimensional modelfun. It would also be a good idea to use the Jacobian option and supply the Jacobian as a sparse matrix. That way, the solver will save time on derivative calculations and also will have information about the separability of the problem into smaller problems.
However, as I mentioned in my last comment to Ioannis, if the solutions from neighboring data sets are expected to be similar, a for-loop can take advantage of that. Conversely, you lose that ability when you rewrite the problem as one big N-dimensional one. The trade-off and which approach will win speed-wise is unclear, though.

Sign in to comment.

Matt J
Matt J on 19 Sep 2013
Edited: Matt J on 19 Sep 2013
I have tried various ways to vectorize the code above and speed things up, but with no success.
It would help to know what you tried. Obviously, if N is the number of pixels, you could write your modelfun in terms of your entire 5xN array X and a 2*N parameter vector beta
function Y=totalmodel(beta,X)
%beta - length 2N vector
% X - 5xN data set
As I mentioned to Lennart, it would probably also be of benefit to use nlinfit's Jacobian option with this. The Jacobian should turn out to be a very sparse block diagonal matrix. By computing it yourself, you would communicate that sparsity to nlinfit, as well as the separable structure of the problem.

Community Treasure Hunt

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

Start Hunting!

Translated by