Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Evaluating a cell array of functions without a loop
Replies: 11   Last Post: Jun 15, 2013 7:43 PM

 Messages: [ Previous | Next ]
 Miguel Alonso Posts: 5 Registered: 1/3/11
Evaluating a cell array of functions without a loop
Posted: Jan 3, 2011 4:06 PM

Hello everyone,

I have this function:

------------------------------------------

function X=Fmain(M,Q)

% M is a nx3 cell array. Its first column contains character strings, and
% its second and third columns contain integers. Q is a 1xm array of reals.

f_array=cellfun(@str2func, M(:,1), 'UniformOutput', false)

% This first block constructs an array of function handles using the character strings % stored in the first column of cell array M. These function handles point to external % functions with two input arguments.

X=[]
for i=1:size(M,1)
X=[X;f_array{i}(Q(M(i,2)),Q(M(i,3)))]
end

% This one evaluates the function handle in each row of f_array, using as inputs the % elements of Q specified in columns 2 and 3 of M, and stacks the outputs in a % single column array.

---------------------------------------

I wonder if there is an alternative that:
a) Avoids the loop, and/or
b) Requires checking M only once, and then constructs a persistent function F(Q) that automatically returns the array X, using the adequate function handles and components of Q specified in M.

Thanks a lot,
Miguel

Date Subject Author
1/3/11 Miguel Alonso
1/3/11 Andy
1/3/11 Miguel Alonso
1/3/11 Oleg Komarov
1/4/11 Miguel Alonso
1/4/11 Oleg Komarov
1/5/11 Miguel Alonso
1/5/11 Miguel Alonso
1/6/11 Oleg Komarov
6/12/13 Catherine
6/12/13 Catherine
6/15/13 Cool Cafe