Are recursively defined or nested anonymous functions dangerous?

조회 수: 2 (최근 30일)
Michael Van de Graaff
Michael Van de Graaff 2022년 5월 22일
댓글: Matt J 2022년 5월 22일
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.

채택된 답변

Matt J
Matt J 2022년 5월 22일
편집: Matt J 2022년 5월 22일
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
Michael Van de Graaff
Michael Van de Graaff 2022년 5월 22일
Also, addThemUp should have a fitObj input in its definition correct?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by