Are recursively defined or nested anonymous functions dangerous?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Michael Van de Graaff
il 22 Mag 2022
Commentato: Matt J
il 22 Mag 2022
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.
Risposta accettata
Matt J
il 22 Mag 2022
Modificato: Matt J
il 22 Mag 2022
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!