Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.