Iterating over a vector of functions in MATLAB

2019-01-18 10:12发布

Is it possible to iterate over a list of functions in MATLAB? I'm trying to test different radial basis functions and this seems like the best way to do it.

2条回答
时光不老,我们不散
2楼-- · 2019-01-18 10:48

If you want to define your own functions it turns out you can do this, following on from gnovice's answer:

funcList = {@(x, y) (x - y), @(x, y) (x + y)}
查看更多
三岁会撩人
3楼-- · 2019-01-18 11:02

You can make a cell array of function handles and iterate over that. For example:

vec = 1:5;                            % A sample vector of values
fcnList = {@max, @min, @mean};        % Functions to apply to the vector
nFcns = numel(fcnList);               % Number of functions to evaluate
result = zeros(1, nFcns);             % Variable to store the results
for iFcn = 1:nFcns
  result(iFcn) = fcnList{iFcn}(vec);  % Get the handle and evaluate it
end
查看更多
登录 后发表回答