Giving Vector Inputs in Function Handles

88 views (last 30 days)
JPS on 27 Apr 2021
Commented: JPS on 29 Apr 2021
I have a function f that takes in 5 inputs and gives an output.
I wish to evaluate this function at different set of points. I have this points stacked up in a matrix called X.
X = X Matrix
I decided to loop through X taking each row at a time as the input for the function f
% defining function handle
f = @(x1 , x2 , x3 , x4 , x5) x1 + x2 + x3 + x4 + x5;
% finding the no of rows
s = size(X);
rows = s(1);
% preallocation of output y
y = zeros(1 , rows);
% looping through X taking one row at a time
for i = 1 : rows
y(i) = f(X(i , :)); % however this line shows an error stating function needs more argument
As mentioned in the comment, MATLAB takes X(i , :) as a single argument.
Now, is there any way to overcome this hurdle?
Of course, I can think of doing,
% looping through X taking one row at a time
for i = 1 : rows
t1 = X(i , 1); t2 = X(i , 2); t3 = X(i , 3); t4 = X(i , 4); t5 = X(i , 5);
y(1) = f(t1 , t2 , t3 , t4 , t5);
But what if there are 50 or 100 variables? This looks time-consuming.
I would be grateful if someone could enlighten me with an elegant method for doing the same.
NOTE: The function f defined above is just for illustrating my problem. Of course, the sum function can be used for this case. But, I was looking for a general solution which can be applied for any multivariable function.
JPS on 29 Apr 2021
@Stephen Cobeldick Thanks a lot! I replaced the in the function handle definitions with . Now it works great!

Sign in to comment.

Accepted Answer

Jan on 28 Apr 2021
Adam danz hit the point alread.
If you have a function with 50 or 100 inputs, this is the design problem already. Your example requires number names of variables, and this is a secure marker of a design fault.
f = @(x1 , x2 , x3 , x4 , x5) x1 + x2 + x3 + x4 + x5;
f = @(x) x(1) + x(2) + x(3) + x(4) + x(5);
Of couse, if only adding is performed, sum(x) is more efficient. But in the general case the solution is to use arrays instead of a bunch of variables.
Compare your code example with Adam's solution to see the point:
y = sum(X,2);
@Adam Danz: I'm finding another answer of you, where I can drop my votes for your valuable explanation. :-)
Steven Lord
Steven Lord on 29 Apr 2021
I agree with the sentiment that many input arguments makes things get ugly very quickly. There's a commonly used function in Optimization Toolbox, fmincon, that has as its longest documented syntax one with ten positional arguments. There have been numerous questions both on Answers and on the CSSM newsgroup before it where people simply forgot to add in one of the inputs and got a somewhat confusing error.
You should probably try to stick closer to the passage from the Book of Armaments detailing the use of the Holy Hand Grenade of Antioch. Five is probably okay rather than being "right out", but many more than that and you probably should switch to a different approach (an options structure, an object, name-value pairs, etc.)

Sign in to comment.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!