转换匿名函数的输入 [英] Convert input of an anonymous function

查看:133
本文介绍了转换匿名函数的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个参数的匿名函数 A 。我需要转换这个函数,所以它需要一个参数,通过改变另一个参数为一个常量。

例如有一个函数:
A = @(X,Y)X + Y;
我想现在有:
B = @(Y)3 + Y;



这在数学中似乎是正常的事情,所以我猜想有一种方法可以在MATLAB中做这样的事情。虽然我找不到解决方案。



我需要这样做的原因是我有一个函数对 A ,但当 A 的参数之一是常量时,还需要解决问题。例如,为 X = 3 找到 A 的最小值。

X 设置为 3 中,但如果你想创建另一个匿名函数,这里是如何做到这一点:

  A = @(X ,Y)X + Y; 
B = @(Y)A(3,Y); %在这里你已经把X = 3

验证:

 >> A(3,4)

ans =

7

>> B(4)

ans =

7


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.

解决方案

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

这篇关于转换匿名函数的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆