2014-03-27 44 views
5

我只是好奇,MATLAB中的所有運算符是否都在內部實現爲函數?對於幾乎所有的MATLAB運算符,我們都有相同的功能plus對於+minus對於-eq對於==transpose對於'MATLAB運算符作​​爲函數

回答

3

是的,這就是MATLAB通過映射中綴運算符到命名函數來實現運算符重載的方法。

documentation列出(按類別)由操作員調用的函數。和more here

+0

感謝您的回答。這[鏈接](http://nf.nci.org.au/facilities/software/Matlab/techdoc/matlab_prog/ch14_o23.html)列出瞭如何重載一個操作符。 – erbal

5

大多數運營商都是由功能來表示的,是的。

提供Implementing Operators for Your Class MathWorks的頁面上徹底名單,轉載這裏:

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的文檔,這具有非常強大適用的任何元素方面的功能虛擬數據複製。


通常有用的是vertcathorizontalvertical串聯用逗號分隔的列表:

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

除了許多其他書面運營商命名的功能,有一對夫婦無證的,你可以訪問(colontranspose,等等。)與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 

然而,注意適當和支持的方式來使用(){},或.是通過subsrefsubasgnsubindex,這取決於上下文。

這些builtins指的是help paren中描述的操作符。同時探索help punct中列出的標點符號。

+0

感謝您的回答。 – erbal

+0

@ user11659我已更新_much_更好的列表和鏈接。 – chappjc

+0

有人知道爲什麼沒有定義'A .- B',而是必須使用'bsxfun(@minus,A,B)'?是否有可能手動重載/定義該操作員? –