matlab中匿名(Anonymous)函数及bsxfun函数

一、匿名函数/Anonymous Function

匿名函数matlab解释:

FUNHANDLE = @FUNCTION_NAME returns a handle to the named function, FUNCTION_NAME. A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (which are often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks). A function handle is one of the standard MATLAB data types. Its class is 'function_handle'.
 
FUNHANDLE = @(ARGLIST)EXPRESSION constructs an anonymous function and returns a handle to that function. The body of the function, to the right of the parentheses, is a single MATLAB expression. ARGLIST is a comma-separated list of input arguments. Execute the function by calling it by means of the returned function handle, FUNHANDLE. For more information on anonymous functions, see "Types of Functions" in the MATLAB Programming documentation.
 
To call the function referred to by a function handle value, use ordinary parenthesis notation.  That is, specify the function handle variable followed by a comma-separated list of input arguments enclosed in parentheses. For example, HANDLE(ARG1, ARG2, ...). To call a function_handle with no arguments, use empty parenthesis, e.g.,
HANDLE().

翻译:

       FUNHANDLE = @FUNCTION_NAME返回命名函数FUNCTION_NAME的句柄。函数句柄是MATLAB值,提供了一种间接调用函数的方法。您可以在对其他函数的调用中传递函数句柄(通常称为函数函数)。您还可以将函数句柄存储在数据结构中,以供以后使用(例如,作为Handle Graphics回调)。函数句柄是标准MATLAB数据类型之一。它的类是“ function_handle”。
      FUNHANDLE = @(ARGLIST)EXPRESSION构造一个匿名函数并返回该函数的句柄。函数主体(在括号的右边)是单个MATLAB表达式。 ARGLIST是输入参数的逗号分隔列表。通过返回的函数句柄FUNHANDLE调用函数来执行该函数。有关匿名函数的更多信息,请参见MATLAB编程文档中的“函数类型”。
       要调用由函数句柄值引用的函数,请使用普通的括号表示法。也就是说,指定函数句柄变量,然后在括号中输入逗号分隔的输入参数列表。例如,HANDLE(ARG1,ARG2,...)。要调用不带参数的function_handle,请使用空括号,例如,HANDLE()

       匿名函数是存储在程序文件中、但与数据类型是 function_handle 的变量相关的函数。匿名函数可以接受输入并返回输出,就像标准函数一样。但是,它们可能只包含一个可执行语句。

下面两种用法的区别在于 函数体的实现在于本处还是其他地方。

注意输入参数/arglist,在匿名函数之前无定义,也不影响使用,在定义后、调用时对输入参数赋值即可。

1、基本用法:

handle = @(arglist)anonymous_function
  • handle为调用匿名函数时使用的名字/函数句柄;
  • arglist为匿名函数的输入参数,可以是一个,也可以是多个,用逗号分隔;
  • anonymous_function为匿名函数的表达式/函数实现;

举例:

f=@(x,y)x^2+y^2;
>> f(1,2)

ans =

     5

放一个具体实现例子:https://www.cnblogs.com/gshang/p/11663727.html

clc,clear,close all;

% 定义匿名函数
K_RBF = @(x,xi,r) exp(-(x-xi).^2./(r.^2));

% 设置变量取值范围
xi = -5:0.01:5;
x = zeros(size(xi));
r = [0.2;0.5;1.0;2.0]*ones(size(xi));

% 画图基础设置
curveType = {'r-','b--','r-.','b-.'};
r_legend = {'r=0.2','r=0.5','r=1.0','r=2.0'};

% 画取不同的 r 值下的函数
for i = 1:length(curveType)
    plot(xi,K_RBF(xi,x,r(i,:)),curveType{i},'linewidth',2);
    hold on
end

% 设置图像参数
set(gca,'fontsize',24),set(gcf,'outerposition',get(0,'screensize'));
legend(r_legend),xlabel('x_i'),ylabel('核函数值K(x,x_i)');
title('RBF核函数在测试点 x=0 处的映射关系');

% 保存图像
print(gcf,'-djpeg','-r300','RBF核函数在测试点 x=0 处的映射关系');

2、基本用法:

handle = @(arglist)anonymous_function

  • handle为调用匿名函数时使用的名字/函数句柄;
  • arglist为匿名函数的输入参数,可以是一个,也可以是多个,用逗号分隔;
  • anonymous_function为匿名函数的函数名/类比普通函数;

举例:

f = @(X) ConstantVelocity(X, T);

其中:ConstantVelocity(X, T)函数实现如下:

% Constant Velocity model for GPS navigation.
function [Val, Jacob] = ConstantVelocity(X, T)

Val = zeros(size(X));
Val(1:2:end) = X(1:2:end) + T * X(2:2:end);
Val(2:2:end) = X(2:2:end);
Jacob = [1,T; 0,1];
Jacob = blkdiag(Jacob,Jacob,Jacob,Jacob);

end

调用如下:

[Xp, ~] = f(Xi);%1

[~, fy] = f(Xp);%2

具体代码:extended Kalman Filter(EKF) for GPS

 

参考:

matlab @匿名函数

Matlab中的匿名函数的使用

Matlab匿名函数

二、bsxfun函数

函数调用形式1:

c = bsxfun(@~, A, B)

%               @plus           Plus 加
%               @minus          Minus 减
%               @times          Array multiply 乘
%               @rdivide        Right array divide 右除
%               @ldivide        Left array divide 左除
%               @power          Array power
%               @max            Binary maximum
%               @min            Binary minimum
%               @rem            Remainder after division
%               @mod            Modulus after division
%               @atan2	        Four-quadrant inverse tangent; result in radians
%               @atan2d	        Four-quadrant inverse tangent; result in dgrees
%               @hypot	        Square root of sum of squares
%               @eq             Equal
%               @ne             Not equal
%               @lt             Less than
%               @le             Less than or equal
%               @gt             Greater than
%               @ge             Greater than or equal
%               @and            Element-wise logical AND
%               @or             Element-wise logical OR
%               @xor            Logical EXCLUSIVE OR

示例:

A=[3 2 1;
   3 2 1;
   3 2 1;];
B=[1;
    1;
    1;];
c = bsxfun(@plus, A, B)  %加法
d = bsxfun(@minus, A, B) %减法
e = bsxfun(@times, A, B) %乘法
f= bsxfun(@rdivide, A, B) %除法右除
g = bsxfun(@ldivide, A, B) %除法 左除

运行结果:

>> test_bsxfun

c =

     4     3     2
     4     3     2
     4     3     2
d =

     2     1     0
     2     1     0
     2     1     0
e =

     3     2     1
     3     2     1
     3     2     1
f =

     3     2     1
     3     2     1
     3     2     1
g =

   0.333333333333333   0.500000000000000   1.000000000000000
   0.333333333333333   0.500000000000000   1.000000000000000
   0.333333333333333   0.500000000000000   1.000000000000000

函数调用形式2:

z = bsxfun(@(x, y) x.*sin(y), x, y);

其中,@(x,y)中为函数输入参数,x.*sin(y)为函数体;

与匿名函数一样,其区别在于bsxfun函数运行效率更高!

参考:

matlab函数 bsxfun浅谈(转载)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值