转换匿名函数的输入 [英] Convert input of an anonymous function
问题描述
我有一个匿名函数 A
带有两个参数.我需要转换这个函数,使其接受一个参数,将另一个参数更改为常量.
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.
例如有一个函数:A = @(X, Y) X + Y;
我现在想要:B = @(Y) 3 + Y;
For example having a function:
A = @(X, Y) X + Y;
I would like now to have:
B = @(Y) 3 + Y;
这在数学上似乎是很正常的事情,所以我猜想在 MATLAB 中有一种方法可以做这样的事情.但是我找不到解决方案.
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.
我需要做这样的事情的原因是我有一个函数可以对A
进行一些计算,但也需要解决A
之一的问题的参数是常数.例如,为 X = 3
找到 A
的最小值.
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
.
推荐答案
您可以使用相同的匿名函数并将 X
作为 3
放入其中,但如果您想创建另一个匿名函数,方法如下:
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
验证:
>> A(3,4)
ans =
7
>> B(4)
ans =
7
这篇关于转换匿名函数的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!