MATLAB运算符作​​为函数 [英] MATLAB operators as functions

查看:93
本文介绍了MATLAB运算符作​​为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇MATLAB中的所有运算符是否都在内部实现为函数?对于几乎所有MATLAB运算符,我们都有等效的函数. plus表示+minus表示-eq表示==transpose表示'.

I was just curious that whether all operators in MATLAB are internally implemented as functions? We have equivalent functions for almost all MATLAB operators. plus for +, minus for -, eq for == and transpose for '.

推荐答案

大多数运算符由函数表示,是的.

Most operators are represented by functions, yes.

MathWorks页面上提供了完整列表为您的班级实施运算符,此处转载:

A thorough list is provided on the MathWorks page Implementing Operators for Your Class, reproduced here:

a + b               plus(a,b)         Binary addition
a - b               minus(a,b)        Binary subtraction
-a                  uminus(a)         Unary minus
+a                  uplus(a)          Unary plus
a.*b                times(a,b)        Element-wise multiplication
a*b                 mtimes(a,b)       Matrix multiplication
a./b                rdivide(a,b)      Right element-wise division
a.\b                ldivide(a,b)      Left element-wise division
a/b                 mrdivide(a,b)     Matrix right division
a\b                 mldivide(a,b)     Matrix left division
a.^b                power(a,b)        Element-wise power
a^b                 mpower(a,b)       Matrix power
a < b               lt(a,b)           Less than
a > b               gt(a,b)           Greater than
a <= b              le(a,b)           Less than or equal to
a >= b              ge(a,b)           Greater than or equal to
a ~= b              ne(a,b)           Not equal to
a == b              eq(a,b)           Equality
a & b               and(a,b)          Logical AND
a | b               or(a,b)           Logical OR
~a                  not(a)            Logical NOT
a:d:b               colon(a,d,b)      Colon operator
a:b
colon(a,b)               
a'                  ctranspose(a)     Complex conjugate transpose
a.'                 transpose(a)      Matrix transpose
command line output display(a)        Display method
[a b]               horzcat(a,b,...)  Horizontal concatenation
[a; b]              vertcat(a,b,...)  Vertical concatenation
a(s1,s2,...sn)      subsref(a,s)      Subscripted reference
a(s1,...,sn) = b    subsasgn(a,s,b)   Subscripted assignment
b(a)                subsindex(a)      Subscript index

查找列表的另一个好地方实际上是 bsxfun的文档,它将任何元素功能与非常强大的虚拟数据复制一起应用.

Another good place to look for a list is actually the documentation for bsxfun, which applies any element-wise function with very powerful virtual data replication.

vertcat通常很有用. 水平

Often useful is vertcat. horizontal vs. vertical concatenation with a comma separated list:

>> c = {'a','b'};
>> horzcat(c{:}) % [c{1} c{2}]
ans =
     ab
>> vertcat(c{:}) % [c{1};c{2}]
ans =
    a
    b


除了许多其他记录在案的具有命名函数(colontranspose等)的运算符外,您还可以使用


In addition to many other documented operators with named functions (colon,transpose,etc.), there are a couple undocumented ones that you can access with builtin:

括号

>> x = [4 5 6];
>> builtin('_paren',x,[2 3])  % x([2 3])
ans =
     5     6

大括号

>> c = {'one','two'};
>> builtin('_brace',c,2)  % c{2}
ans =
two

结构字段访问(点)

>> s = struct('f','contents');
>> builtin('_dot',s,'f')  % s.f
ans =
contents

但是,请注意,使用(){}.的正确且受支持的方法是通过

However, note that the proper and supported way to use (), {}, or . is via subsref, subasgn, and subindex, depending on the context.

这些内置函数引用help paren中描述的运算符.同时浏览help punct中列出的标点符号.

These builtins refer to the operators described in help paren. Also explore the punctuation listed in help punct.

这篇关于MATLAB运算符作​​为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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