Are recursively defined or nested anonymous functions dangerous?

I'm writing a generic fitting app for myself. My data are 2D images of atomic density distributions. They have several features on them, such as two peaks and a background offset. Each feature is an object (featureObj) which contains the parameters and a function handle. all the festures are contained in another object (fitObj). I wish to generate a function that takes all the parameters for all the features and divides them into their respective individual feature functions according to an index list contained by each featureObj
So for this example case I have
fitObj.featureObj(1).featfun = @(params) fPeak1(params,XY_coords); % the function handle f1 for feature1 which is a peak
fitObj.featureObj(2).featfun = @(params) fPeak2(params,XY_coords); % the function handle f2 for feature2 which is a peak
fitObj.featureObj(3).featfun = @(params) params*ones(size(XY_coords)); % the function handle f3 for feature3 which is a uniform background offset
% now i build the composite fit function explicitly from the three features
fitObj.fitfun_composite = @(all_params) fitObj.featureObj(1).featfun(all_params(featureObj(1).param_indices))...
+ fitObj.featureObj(2).featfun(all_params(featureObj(2).param_indices))...
+ fitObj.featureObj(3).featfun(all_params(featureObj(3).param_indices))
% the featureObj holds the indices for each feature. if each peak takes
% five parameters and the background takes only one, then
% featureObj(1).param_indices = 1:5 and featureObj(2).param_indices = 6:10
% and featureObj(3).param_indices = 11
And so that's all fine. My question is, if i have an arbitrary number of featureObjects (to characterize a lot of peaks in the density data), is it problematic to define the composite fit function as
nfeatures = length(fitObj)
fitObj.fitfun_composite =@(all_params) 0*all_params;
for ii = 1:nfeatures
fitObj.fitfun_composite = @(all_params) fitObj.fitfun_composite(all_params)...
+ fitObj.featureObj(ii).featfun(all_params(featureObj(ii).param_indices));
end
This seems to work, but I don't know if there is a trap here or if i'm missing a more obvious/elegant/efficient way.

 Accepted Answer

I don't know about dangerous, but it is definitely inefficient and harder to debug. It will run much faster if you just implement it as a multi-line function:
fitObj.fitfun_composite = @(all_params) addThemUp(fitObj,all_params);
function accum = addThemUp(fitObj,all_params)
accum=0;
for i = 1:nfeatures
accum=accum+fitObj.featureObj(i).featfun(all_params(featureObj(i).param_indices));
end
end

4 Comments

I agree, the loop at execution time is notably more efficient and readable.
Fantastic!
Thank you again Matt J, you're saving me all sorts of trouble this weekend.
I've been using matlab for eight years and there're still all sorts of details I'm learning.
I'm glad I asked the question an hour ago, because I used my original method and immediatly ran out of memory. So I came back and poof! you helped me out.
I think my conceptual struggle was to remember I can add function handles together like that.
Also, addThemUp should have a fitObj input in its definition correct?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!