Iterating over a vector of functions in MATLAB

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.

Answers


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

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)}

Need Your Help

What is the difference between the /Ox and /O2 compiler options?

c++ visual-studio visual-c++ compiler-optimization compiler-options

Microsoft's C++ compiler (cl.exe, as included with Visual Studio) offers several optimization switches. The difference between most of them seems self-explanatory, but it's not clear to me what the