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 '
.
The colon(:) is one of the most useful operator in MATLAB. It is used to create vectors, subscript arrays, and specify for iterations. If you want to create a row vector, containing integers from 1 to 10, you write − 1:10.
It means not equal to as you say. It doesn't have any other meaning in MATLAB. The ~ by itself has meaning. It can be used to not return certain outputs from a function.
It consists of a single MATLAB expression and any number of input and output arguments. You can define an anonymous function right at the MATLAB command line or within a function or script. This way you can create simple functions without having to create a file for them.
* is matrix multiplication while . * is elementwise multiplication. In order to use the first operator, the operands should obey matrix multiplication rules in terms of size.
Most operators are represented by functions, yes.
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
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.
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
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
:
parenthesis
>> x = [4 5 6];
>> builtin('_paren',x,[2 3]) % x([2 3])
ans =
5 6
curly braces
>> c = {'one','two'};
>> builtin('_brace',c,2) % c{2}
ans =
two
struct field access (dot)
>> 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.
These builtins refer to the operators described in help paren
. Also explore the punctuation listed in help punct
.
Yes, that's how MATLAB enables operator overloading, by mapping infix operators to named functions.
The documentation lists (by category) the functions invoked by operators. And more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With