Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 581
Dean Barnes Avatar asked Apr 14 '11 20:04

Dean Barnes


People also ask

How do I iterate a vector in MATLAB?

Note that Matlab iterates through the columns of list , so if list is a nx1 vector, you may want to transpose it. If you don't know whether list is a column or a row vector, you can use the rather ugly combination (:)' : for elm = list(:)'; %... ;end .

What does %% mean in MATLAB?

Description: The percent sign is most commonly used to indicate nonexecutable text within the body of a program. This text is normally used to include comments in your code. Some functions also interpret the percent sign as a conversion specifier.

How do I run an iteration in MATLAB?

Use the up-arrow key, followed by the enter or return key, to iterate, or repeatedly execute, this statement: x = sqrt(1 + x) Here is what you get when you start with x = 3. These values are 3, / 1 + 3, √ 1 + / 1 + 3, √ 1 + √ 1 + / 1+3, and so on.

Is there a do loop in MATLAB?

Basically there is no do while loop in Matlab like c programming, cpp programming, and other programming languages. But instead of using do while loop works powerfully in Matlab.


1 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
like image 146
gnovice Avatar answered Sep 28 '22 11:09

gnovice