Giving Vector Inputs in Function Handles
Ältere Kommentare anzeigen
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 = 

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
end
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);
end
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.
3 Kommentare
Why not just change the anonymous function to this?
f = @(x)sum(x);
but if that's the case, the whole thing can be reduced to
y = sum(X,2);
If that's just a example and not the real anonymous function, perhaps you could provide more details.
Stephen23
am 29 Apr. 2021
"But what if there are 50 or 100 variables?"
Then your code has a major design flaw.
"This looks time-consuming."
It would be time consuming to write out, difficult to write without mistakes, and very difficult to maintain.
"I would be grateful if someone could enlighten me with an elegant method for doing the same."
Using numbered variables names is a sign that you are doing something wrong.
The much better solution is to use a vector, just like MATLAB was designed for.
JPS
am 29 Apr. 2021
Akzeptierte Antwort
Weitere Antworten (1)
I had a similar thing recently. The thing to do is to set the @(___) to a vector variable such as @(a) and then when making the function, use a(index) where needed.
So for this problem:
f1 = @(a) a(1) + a(2) + a(3) + a(4) + a(5);
% or
f2 = @(a) sum(a)
example_f1 = f1([-9 3 10 25 -7])
example_f2 = f2([2 6 4 -9 3 9 10 25 -7 -22])
For a problem with a specifc number of values, function one would work well since you'd be able to specify the variables. However, as you said there could be a TON of value that would be a pain to total, so an encompassing function like function 2 would be best.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
