Filip Hazubski
Filip Hazubski

Reputation: 1728

Convert input of an anonymous function

I have an anonymous function A taking two arguments. I need to convert this function so it takes one argument, by changing the other argument to a constant.

For example having a function: A = @(X, Y) X + Y; I would like now to have: B = @(Y) 3 + Y;

This seems to be a normal thing to do in mathematics, so I guess there is a way to do such thing in MATLAB. I cannot find the solution though.

The reason I need to do something like this is that I have a function that does some calculations on A, but also needs to solve problems when one of the A's arguments is constant. For example find a minimum of the A for X = 3.

Upvotes: 0

Views: 94

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

You can use the same anonymous function and put X as 3 in it but if you want to create another anonymous function, here is how to do that:

A = @(X, Y) X + Y;   
B = @(Y) A(3,Y);     %Here you have put X=3

To verify:

>> A(3,4)

ans =

     7

>> B(4)

ans =

     7

Upvotes: 2

Related Questions