Convert input of an anonymous function

邮差的信 提交于 2019-12-25 12:11:51

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/47486471/convert-input-of-an-anonymous-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!