问题描述
|
是否可以迭代MATLAB中的函数列表?我正在尝试测试不同的径向基函数,这似乎是最好的方法。
解决方法
您可以使一个函数句柄的单元格数组并对其进行迭代。例如:
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
,如果您想定义自己的函数,那么可以按照gnovice的回答进行操作:
funcList = {@(x,y) (x - y),@(x,y) (x + y)}