en fr

Disponible uniquement en anglais

Sysquake Pro – Table of Contents

Sysquake – Table of Contents

Sysquake for LaTeX – Table of Contents

Operators

Operators are special functions with a syntax which mimics mathematical arithmetic operations like the addition and the multiplication. They can be infix (such as x+y), separating their two arguments (called operands); prefix (such as -x), placed before their unique operand; or postfix (such as M'), placed after their unique operand. In Sysquake, their arguments are always evaluated from left to right. Since they do not require parenthesis or comma, their priority matters. Priority specifies when subexpressions are considered as a whole, as the argument of some operator. For instance, in the expression a+b*c, where * denotes the multiplication, the evaluation could result in (a+b)*c or a+(b*c); however, since operator *'s priority is higher than operator +'s, the expression yields a+(b*c) without ambiguity.

Here is the list of operators, from higher to lower priority:

'  .'
^  .^
- (unary)
*  .*  /  ./  \  .\
+  -
==  ~=  <  >  <=  >= === ~==
~
&
|
&&
||
:  ?
,
;

Most operators have also a functional syntax; for instance, a+b can also be written plus(a,b). This enables their overriding with new definitions and their use in function references or functions such as feval which take the name of a function as an argument.

Here is the correspondence between operators and functions:

[a;b]vertcat(a,b)
[a,b]horzcat(a,b)
a:bcolon(a,b)
a:b:ccolon(a,b,c)
a|bor(a,b)
a&band(a,b)
a<=ble(a,b)
a<blt(a,b)
a>=bge(a,b)
a>bgt(a,b)
a==beq(a,b)
a~=bne(a,b)
a===bsame(a,b)
a~==bunsame(a,b)
a+bplus(a,b)
 
a-bminus(a,b)
a*bmtimes(a,b)
a/bmrdivide(a,b)
a\bmldivide(a,b)
a.*btimes(a,b)
a./brdivide(a,b)
a.\bldivide(a,b)
a^bmpower(a,b)
a.^bpower(a,b)
~anot(a)
-auminus(a)
+auplus(a)
a'ctranspose(a)
a.'transpose(a)
  

Operator which do not have a corresponding function are ?:, && and || because unlike functions, they do not always evaluate all of their operands.

Operator ()

Parenthesis.

Syntax

(expr)
v(:)
v(index)
v(index1, index2)
v(:, index)
v(index, :)
v(select)
v(select1, select2)
v(:,:)

Description

A pair of parenthesis can be used to change the order of evaluation. The subexpression it encloses is evaluated as a whole and used as if it was a single object. Parenthesis serve also to indicate a list of input or output parameters; see the description of the function keyword.

The last use of parenthesis is for specifying some elements of an array or list variable.

Arrays: In LME, any numeric object is considered as an array of two dimensions or more. Therefore, at least two indices are required to specify a single element; the first index specifies the row, the second the column, and so on. In some circumstances, however, it is convenient to consider an array as a vector, be it a column vector, a row vector, or even a matrix whose elements are indexed row-wise (or on some platforms). For this way of handling arrays, a single index is specified.

The first valid value of an index is always 1. The array whose elements are extracted is usually a variable, but can be any expression: an expression like [1,2;3,4](1,2) is valid and gives the 2nd element of the first row, i.e. 3.

In all indexing operations, several indices can be specified simultaneously to extract more than one element along a dimension. A single colon means all the elements along the corresponding dimension.

Instead of indices, the elements to be extracted can be selected by the true values in a logical array of the same size as the variable (the result is a column vector), or in a logical vector of the same size as the corresponding dimension. Calculating a boolean expression based on the variable itself used as a whole is the easiest way to get a logical array.

Variable indexing can be used in an expression or in the left hand side of an assignment. In this latter case, the right hand size can be one of the following:

  • An array of the same size as the extracted elements.
  • A scalar, which is assigned to each selected element of the variable.
  • An empty matrix [], which means that the selected elements should be deleted. Only whole rows or columns (or (hyper)planes for arrays of more dimensions) can be deleted; i.e. a(2:5,:) = [] and b([3,6:8]) = [] (if b is a row or column vector) are legal, while c(2,3) = [] is not.

When indices are larger than the dimensions of the variable, the variable is expanded; new elements are set to 0 for numeric arrays, false for logical arrays, the nul character for character array, and the empty array [] for cell arrays.

Lists: In LME, lists have one dimension; thus a single index is required. Be it with a single index or a vector of indices, indexed elements are grouped in a list. New elements, also provided in a list, can be assigned to indexed elements; if the list to be assigned has a single element, the element is assigned to every indexed element of the variable.

Cell arrays: cell arrays are subscripted like other arrays. The result, or the right-hand side of an assignment, is also a cell array, or a list for the syntax v(select) (lists are to cell arrays what column vectors are to non-cell arrays). To create a single logical array for selecting some elements, function cellfun may be useful. To remove cells, the right-hand side of the assignment can be the empty list {} or the empty array [].

Structure arrays: access to structure array fields combines subscripting with parenthesis and structure field access with dot notation. When the field is not specified, parenthesis indexing returns a structure or structure array. When indexing results in multiple elements and a field is specified, the result is a value sequence.

Examples

Ordering evaluation:

(1+2)*3
  9

Extracting a single element, a row, and a column:

a = [1,2,3; 4,5,6];
a(2,3)
  6
a(2,:)
  4 5 6
a(:,3)
  3
  6

Extracting a sub-array with contiguous rows and non-contiguous columns:

a(1:2,[1,3])
  1 3
  4 6

Array elements as a vector:

a(3:5)
  3
  4
  5
a(:)
  1
  2
  3
  4
  5
  6

Selections of elements where a logical expression is true:

a(a>=5)
  5
  6
a(:, sum(a,1) > 6)
  2 3
  5 6

Assignment:

a(1,5) = 99
  a =
    1 2 3 0 99
    4 5 6 0 0

Extraction and assignment of elements in a list:

a = {1,[2,7,3],'abc',magic(3),'x'};
a([2,5])
  {[2,7,3],'x'}
a([2,5]) = {'ab','cde'}
  a =
    {1,'ab','abc',[8,1,6;3,5,7;4,9,2],'cde'}
a([2,5]) = {[3,9]}
  a =
    {1,[3,9],'abc',[8,1,6;3,5,7;4,9,2],[3,9]}

Removing elements in a list ({} and [] have the same effect here):

a(4) = {}
  a =
    {1,[3,9],'abc',[3,9]}
a([1, 3]) = []
  a =
    {[3,9],[3,9]}

Replacing NaN with empty arrays in a cell array:

C = {'abc', nan; 2, false};
C(cellfun(@(x) any(isnan(x(:))), C)) = {[]};

Element in a structure array:

SA = structarray('a',{1,[2,3]},'b',{'ab','cde'});
SA(1).a
  2 3
SA(2).b = 'X';

When assigning a new field and/or a new element of a structure array, the new field is added to each element and the size of the array is expanded; fields are initialized to the empty array [].

SA(3).c = true;
SA(1).c
  []

See also

Operator {}, operator ., end, reshape, variable assignment, operator [], subsref, subsasgn, cellfun

Operator []

Brackets.

Syntax

[matrix_elements]

Description

A pair of brackets is used to define a 2-d array given by its elements or by submatrices. The operator , (or spaces) is used to separate elements on the same row, and the operator ; (or newline) is used to separate rows. Since the space is considered as a separator when it is in the direct scope of brackets, it should not be used at the top level of expressions; as long as this rule is observed, each element can be given by an expression.

Inside brackets, commas and semicolons are interpreted as calls to horzcat and vertcat. Brackets themselves have no other effect than changing the meaning of commas, semicolons, spaces, and new lines: the expression [1], for instance, is strictly equivalent to 1. The empty array [] is a special case.

Since horzcat and vertcat also accept cell arrays, brackets can be used to concatenate cell arrays, too.

Examples

[1, 2, 3+5]
  1 2 8
[1:3; 2 5 , 9 ]
  1 2 3
  2 5 9
[5-2, 3]
  3 3
[5 -2, 3]
  5 -2 3
[(5 -2), 3]
  3 3
[1 2
3 4]
  1 2
  3 4
[]
  []

Concatenation of two cell arrays:

C1 = {1; 2};
C2 = {'ab'; false};
[C1, C2]
  2x2 cell array

Compare this with the effect of braces, where elements are not concatenated but used as cells:

{C1, C2}
  1x2 cell array

See also

Operator {}, operator (), operator ,, operator ;

Operator {}

Braces.

Syntax

{list_elements}
{cells}
{struct_elements}
v{index}
v{index1, index2, ...}
v{index} = expr
fun(...,v{:},...)

Description

A pair of braces is used to define a list, a cell array, a struct, or an n-by-1 struct array given by its elements. When no element has a name (a named element is written name=value where value can be any expression), the result is a list or a celle array; when all elements have a name, the result is a struct or a struct array.

In a list, the operator , is used to separate elements. In a cell array, the operator , is used to separate cells on the same row; the operator ; is used to separate rows. Braces without semicolons produce a list; braces with semicolon(s) produce a cell array.

In a struct, the operator , is used to separate fields. In a struct array, the operator ; is used to separate elements.

v{index} is the element of list variable v whose index is given. index must be an integer between 1 (for the first element) and length(v) (for the last element). v{index} may be used in an expression to extract an element, or on the left hand-side of the equal sign to assign a new value to an element. Unless it is the target of an assignment, v may also be the result of an expression. If v is a cell array, v{index} is the element number index.

v{index1,index2,...} gives the specified cell of a cell array.

v itself may be an element or a field in a larger variable, provided it is a list; i.e. complicated assignments like a{2}.f{3}(2,5)=3 are accepted. In an assignment, when the index (or indices) are larger than the list or cell array size, the variable is expanded with empty arrays [].

In the list of the input arguments of a function call, v{:} is replaced with its elements. v may be a list variable or the result of an expression.

Examples

x = {1, 'abc', [3,5;7,1]}
  x =
    {1,string,real 2x2}
x{3}
    3 5
    7 1
x{2} = 2+3j
  x =
    {1,2+3j,real 2x2}
x{3} = {2}
  x =
    {1,2+3j,list}
x{end+1} = 123
  x =
    {1,2+3j,list,123}
C = {1, false; 'ab', magic(3)}
  2x2 cell array
C{2, 1}
  ab
a = {1, 3:5};
fprintf('%d ', a{:}, 99);
  1 3 4 5 99
s = {a=1, b='abc'};
s.a
  1
S = {a=1, b='abc'; a=false, b=1:5};
size(S)
  2 1
S(2).b
  1 2 3 4 5
S = {a=1; b=2};
S(1).b
  []

See also

operator ,, operator [], operator (), operator ;, operator ., subsref, subsasgn

Operator . (dot)

Structure field access.

Syntax

v.field
v.field = expr

Description

A dot is used to access a field in a structure. In v.field, v is the name of a variable which contains a structure, and field is the name of the field. In expressions, v.field gives the value of the field; it is an error if it does not exist. As the target of an assignment, the value of the field is replaced if it exists, or a new field is added otherwise; if v itself is not defined, a structure is created from scratch.

v itself may be an element or a field in a larger variable, provided it is a structure (or does not exists in an assignment); i.e. complicated assignments like a{2}.f{3}(2,5)=3 are accepted.

If V is a structure array, V.field is a value sequence which contains the specified field of each element of V.

The syntax v.(expr) permits to specify the field name dynamically at run-time, as the result of evaluating expression expr. v('f') is equivalent to v.f. This syntax is more elegant than functions getfield and setfield.

Examples

s.f = 2
  s =
    f: 2
s.g = 'hello'
  s =
    f: 2
    s: string
s.f = 1:s.f
  s =
    f: real 1x2
    g: string

See also

Operator (), operator {}, getfield, setfield, subsref, subsasgn

Operator +

Addition.

Syntax

x + y
M1 + M2
M + x
plus(x, y)
+x
+M
uplus(x)

Description

With two operands, both operands are added together. If both operands are matrices with a size different from 1-by-1, their size must be equal; the addition is performed element-wise. If one operand is a scalar, it is added to each element of the other operand.

With one operand, no operation is performed, except that the result is converted to a number if it was a string or a logical value, like with all mathematical operators and functions. For strings, each character is replaced with its numeric encoding. The prefix + is actually a synonym of double.

plus(x,y) is equivalent to x+y, and uplus(x) to +x. They can be used to redefine these operators for objects.

Example

2 + 3
  5
[1 2] + [3 5]
  4 7
[3 4] + 2
  5 6

See also

operator -, sum, addpol, double

Operator -

Subtraction or negation.

Syntax

x - y
M1 - M2
M - x
minus(x, y)
-x
-M
uminus(x)

Description

With two operands, the second operand is subtracted from the first operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the subtraction is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.

With one operand, the sign of each element is changed.

minus(x,y) is equivalent to x-y, and uminus(x) to -x. They can be used to redefine these operators for objects.

Example

2 - 3
  -1
[1 2] - [3 5]
  -2 -3
[3 4] - 2
  1 2
-[2 2-3j]
  -2 -2+3j

See also

operator +, conj

Operator *

Matrix multiplication.

Syntax

x * y
M1 * M2
M * x
mtimes(x, y)

Description

x*y multiplies the operands together. Operands can be scalars (plain arithmetic product), matrices (matrix product), or mixed scalar and matrix.

mtimes(x,y) is equivalent to x*y. It can be used to redefine this operator for objects.

Example

2 * 3
  6
[1,2;3,4] * [3;5]
  13
  29
[3 4] * 2
  6 8

See also

operator .*, operator /, prod

Operator .*

Scalar multiplication.

Syntax

x .* y
M1 .* M2
M .* x
times(x, y)

Description

x.*y is the element-wise multiplication. If both operands are matrices with a size different from 1-by-1, their size must be equal; the multiplication is performed element-wise. If one operand is a scalar, it multiplies each element of the other operand.

times(x,y) is equivalent to x.*y. It can be used to redefine this operator for objects.

Example

[1 2] .* [3 5]
  3 10
[3 4] .* 2
  6 8

See also

operator *, operator ./, operator .^

Operator /

Matrix right division.

Syntax

a / b
A / B
A / b
mrdivide(a, b)

Description

a/b divides the first operand by the second operand. If the second operand is a scalar, it divides each element of the first operand. Otherwise, it must be a square matrix; M1/M2 is equivalent to M1*inv(M2).

mrdivide(x,y) is equivalent to x/y. It can be used to redefine this operator for objects.

Example

9 / 3
  3
[2,6] / [1,2;3,4]
  5 -1
[4 10] / 2
  2 5

See also

operator \, inv, operator ./, deconv

Operator ./

Scalar right division.

Syntax

x ./ y
M1 ./ M2
M ./ x
x ./ M
rdivide(x, y)

Description

The first operand is divided by the second operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the division is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.

rdivide(x,y) is equivalent to x./y. It can be used to redefine this operator for objects.

Examples

[3 10] ./ [3 5]
  1 2
[4 8] ./ 2
  2 4
10 ./ [5 2]
  2 5

See also

operator /, operator .*, operator .^

Operator \

Matrix left division.

Syntax

x \ y
M1 \ M2
x \ M
mldivide(x, y)

Description

x\y divides the second operand by the first operand. If the first operand is a scalar, it divides each element of the second operand. Otherwise, it must be a square matrix; M1\M2 is equivalent to inv(M1)*M2.

mldivide(x,y) is equivalent to x\y. It can be used to redefine this operator for objects.

Examples

3 \ 9
  3
[1,2;3,4] \ [2;6]
  2
  0
2 \ [4 10]
  2 5

See also

operator /, inv, operator .\

Operator .\

Scalar left division.

Syntax

M1 .\ M2
M1 .\ x
ldivide(x, y)

Description

The second operand is divided by the first operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the division is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.

ldivide(x,y) is equivalent to x.\y. It can be used to redefine this operator for objects.

Example

[1 2 3] .\ [10 11 12]
  10 5.5 4

See also

operator \, operator ./

Operator ^

Matrix power.

Syntax

x ^ y
M ^ y
x ^ M
mpower(x, y)

Description

x^y calculates x to the y power, provided that either

  • both operands are scalar;
  • the first operand is a square matrix and the second operand is a scalar;
  • or the first operand is a scalar and the second operand is a square matrix.

Other cases yield an error.

mpower(x,y) is equivalent to x^y. It can be used to redefine this operator for objects.

Examples

2 ^ 3
  8
[1,2;3,4] ^ 2
  7  10
  15 22
2 ^ [1,2;3,4]
  10.4827 14.1519
  21.2278 31.7106

Algorithms

If the first operand is a scalar and the second a square matrix, the matrix exponential is used. The result is expm(log(x)*M).

If the first operand is a square matrix and the second a scalar, unless for small real integers, the same algorithm as for matrix functions is used, i.e. a complex Schur decomposition followed by the Parlett method. The result is funm(M, @(x) x^y).

See also

operator .^, expm, funm

Operator .^

Scalar power.

Syntax

M1 .^ M2
x .^ M
M .^ x
power(x, y)

Description

M1.^M2 calculates M1 to the M2 power, element-wise. Both arguments must have the same size, unless one of them is a scalar.

power(x,y) is equivalent to x.^y. It can be used to redefine this operator for objects.

Examples

[1,2;3,4].^2
  1  4
  9 16
[1,2,3].^[5,4,3]
  1 16 27

See also

operator ^, exp

Operator '

Complex conjugate transpose.

Syntax

M'
ctranspose(M)

Description

M' is the transpose of the real matrix M, i.e. columns and rows are permuted. If M is complex, the result is the complex conjugate transpose of M. If M is an array with multiple dimensions, the first two dimensions are permuted.

ctranspose(M) is equivalent to M'. It can be used to redefine this operator for objects.

Examples

[1,2;3,4]'
  1 3
  2 4
[1+2j, 3-4j]'
  1-2j
  3+4j

See also

operator .', conj

Operator .'

Transpose.

Syntax

M.'
transpose(M)

Description

M.' is the transpose of the matrix M, i.e. columns and rows are permuted. M can be real or complex. If M is an array with multiple dimensions, the first two dimensions are permuted.

transpose(M) is equivalent to M.'. It can be used to redefine this operator for objects.

Example

[1,2;3,4].'
  1 3
  2 4
[1+2j, 3-4j].'
  1+2j
  3-4j

See also

operator ', permute, fliplr, flipud, rot90

Operator ==

Equality.

Syntax

x == y
eq(x, y)

Description

x == y is true if x is equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

eq(x,y) is equivalent to x==y. It can be used to redefine this operator for objects.

Example

1 == 1
  true
1 == 1 + eps
  false
1 == 1 + eps / 2
  true
inf == inf
  true
nan == nan
  false
[1,2,3] == [1,3,3]
  T F T

See also

operator ~=, operator <, operator <=, operator >, operator >=, operator ===, operator ~==, strcmp

Operator ===

Object equality.

Syntax

a === b
same(a, b)

Description

a === b is true if a is the same as b, and false otherwise. a and b must have exactly the same internal representation to be considered as equal; with IEEE floating-point numbers, nan===nan is true and 0===-0 is false. Contrary to the equality operator ==, === returns a single boolean even if its operands are arrays.

same(a,b) is equivalent to a===b.

Example

(1:5) === (1:5)
  true
(1:5) == (1:5)
  T T T T T
[1,2,3] === [4,5]
  false
[1,2,3] == [4,5]
  Incompatible size
nan === nan
  true
nan == nan
  false

See also

operator ~==, operator ==, operator ~=, operator <, operator <=, operator >, operator >=, operator ==, operator ~=, strcmp

Operator ~=

Inequality.

Syntax

x ~= y
ne(x, y)

Description

x ~= y is true if x is not equal to y, and false otherwise. Comparing NaN (not a number) to any number yields true, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

ne(x,y) is equivalent to x~=y. It can be used to redefine this operator for objects.

Example

1 ~= 1
  false
inf ~= inf
  false
nan ~= nan
  true
[1,2,3] ~= [1,3,3]
  F T F

See also

operator ==, operator <, operator <=, operator >, operator >=, operator ===, operator ~==, strcmp

Operator ~==

Object inequality.

Syntax

a ~== b
unsame(a, b)

Description

a ~== b is true if a is not the same as b, and false otherwise. a and b must have exactly the same internal representation to be considered as equal; with IEEE numbers, nan~==nan is false and 0~==-0 is true. Contrary to the inequality operator, ~== returns a single boolean even if its operands are arrays.

unsame(a,b) is equivalent to a~==b.

Example

(1:5) ~== (1:5)
  false
(1:5) ~= (1:5)
  F F F F F
[1,2,3] ~== [4,5]
  true
[1,2,3] ~= [4,5]
  Incompatible size
nan ~== nan
  false
nan ~= nan
  true

See also

operator ===, operator ==, operator ~=, operator <, operator <=, operator >, operator >=, strcmp

Operator <

Less than.

Syntax

x < y
lt(x, y)

Description

x < y is true if x is less than y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

lt(x,y) is equivalent to x<y. It can be used to redefine this operator for objects.

Example

[2,3,4] < [2,4,2]
  F T F

See also

operator ==, operator ~=, operator <=, operator >, operator >=

Operator >

Greater than.

Syntax

x > y
gt(x, y)

Description

x > y is true if x is greater than y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

gt(x,y) is equivalent to x>y. It can be used to redefine this operator for objects.

Example

[2,3,4] > [2,4,2]
  F F T

See also

operator ==, operator ~=, operator <, operator <=, operator >=

Operator <=

Less or equal to.

Syntax

x <= y
le(x, y)

Description

x <= y is true if x is less than or equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

le(x,y) is equivalent to x<=y. It can be used to redefine this operator for objects.

Example

[2,3,4] <= [2,4,2]
  T T F

See also

operator ==, operator ~=, operator <, operator >, operator >=

Operator >=

Greater or equal to.

Syntax

x >= y
ge(x, y)

Description

x >= y is true if x is greater than or equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is an array, the comparison is performed element-wise and the result has the same size.

ge(x,y) is equivalent to x>=y. It can be used to redefine this operator for objects.

Example

[2,3,4] >= [2,4,2]
  T F T

See also

operator ==, operator ~=, operator <, operator <=, operator >

Operator ~

Not.

Syntax

~b
not(b)

Description

~b is false (logical 0) if b is different from 0 or false, and true otherwise. If b is an array, the operation is performed on each element.

not(b) is equivalent to ~b. It can be used to redefine this operator for objects.

Character ~ can also be used as a placeholder for unused arguments.

Examples

~true
  false
~[1,0,3,false]
  F T F T

See also

operator ~=, bitcmp, function (unused arguments)

Operator &

And.

Syntax

b1 & b2
and(b1, b2)

Description

b1&b2 performs the logical AND operation between the corresponding elements of b1 and b2; the result is true (logical 1) if both operands are different from false or 0, and false (logical 0) otherwise.

and(b1,b2) is equivalent to b1&b2. It can be used to redefine this operator for objects.

Example

[false, false, true, true] & [false, true, false, true]
  F F F T

See also

operator |, xor, operator ~, operator &&, all

Operator &&

And with lazy evaluation.

Syntax

b1 && b2

Description

b1&&b2 is b1 if b1 is false, and b2 otherwise. Like with if and while statements, b1 is true if it is a nonempty array with only non-zero elements. b2 is evaluated only if b1 is true.

b1&&b2&&...&&bn returns the last operand which is false (remaining operands are not evaluated), or the last one.

Example

Boolean value which is true if the vector v is made of pairs of equal values:

mod(length(v),2) == 0 && v(1:2:end) == v(2:2:end)

The second operand of && is evaluated only if the length is even.

See also

operator ||, operator ?, operator &, if

Operator |

Or.

Syntax

b1 | b2
or(b1, b2)

Description

b1|b2 performs the logical OR operation between the corresponding elements of b1 and b2; the result is false (logical 0) if both operands are false or 0, and true (logical 1) otherwise.

or(b1,b2) is equivalent to b1|b2. It can be used to redefine this operator for objects.

Example

[false, false, true, true] | [false, true, false, true]
  F T T T

See also

operator &, xor, operator ~, operator ||, any

Operator ||

Or with lazy evaluation.

Syntax

b1 || b2

Description

b1||b2 is b1 if b1 is true, and b2 otherwise. Like with if and while statements, b1 is true if it is a nonempty array with only non-zero elements. b2 is evaluated only if b1 is false.

b1||b2||...||bn returns the last operand which is true (remaining operands are not evaluated), or the last one.

Example

Boolean value which is true if the vector v is empty or if its first element is NaN:

isempty(v) || isnan(v(1))

See also

operator &&, operator ?, operator |, if

Operator ?

Alternative with lazy evaluation.

Syntax

b ? x : y

Description

b?x:y is x if b is true, and y otherwise. Like with if and while statements, b is true if it is a nonempty array with only non-zero elements. Only one of x and y is evaluated depending on b.

Operators ? and : have the same priority; parenthesis or brackets should be used if e.g. x or y is a range.

Example

Element of a vector v, or default value 5 if the index ind is out of range:

ind < 1 || ind > length(v) ? 5 : v(ind)

See also

operator &&, operator ||, if

Operator ,

Horizontal matrix concatenation.

Syntax

[M1, M2, ...]
[M1 M2 ...]
horzcat(M1, M2, ...)

Description

Between brackets, the comma is used to separate elements on the same row in a matrix. Elements can be scalars, vector, arrays, cell arrays, or structures; their number of rows must be the same, unless one of them is an empty array. For arrays with more than 2 dimensions, all dimensions except dimension 2 (number of columns) must match.

Outside brackets or between parenthesis, the comma is used to separate statements or the arguments of functions.

horzcat(M1,M2,...) is equivalent to [M1,M2,...]. It can be used to redefine this operator for objects. It accepts any number of input arguments; horzcat() is the real double empty array [], and horzcat(M) is M.

Between braces, the comma separates cells on the same row.

Examples

[1,2]
  1 2
[[3;5],ones(2)]
  3 1 1
  5 1 1
['abc','def']
  abcdef

See also

operator [], operator ;, cat, join, operator {}

Operator ;

Vertical matrix concatenation.

Syntax

[M1; M2]
vertcat(M1, M2)

Description

Between brackets, the semicolon is used to separate rows in a matrix. Rows can be scalars, vector, arrays, cell arrays, or structures; their number of columns must be the same, unless one of them is an empty array. For arrays with more than 2 dimensions, all dimensions except dimension 1 (number of rows) must match.

Outside brackets, the comma is used to separate statements; they loose any meaning between parenthesis and give a syntax error.

vertcat(M1,M2) is equivalent to [M1;M2]. It can be used to redefine this operator for objects.

Between braces, the semicolon separates rows of cells.

Examples

[1;2]
  1
  2
[1:5;3,2,4,5,1]
  1 2 3 4 5
  3 2 4 5 1
['abc';'def']
  abc
  def

See also

operator [], operator ,, join, operator {}

Operator :

Range.

Syntax

x1:x2
x1:step:x2
colon(x1,x2)
colon(x1,step,x2)

Description

x1:x2 gives a row vector with the elements x1, x1+1, x1+2, etc. until x2. The last element is equal to x2 only if x2-x1 is an integer, and smaller otherwise. If x2<x1, the result is an empty matrix.

x1:step:x2 gives a row vector with the elements x1, x1+step, x1+2*step, etc. until x2. The last element is equal to x2 only if (x2-x1)/step is an integer. With fractional numbers, rounding errors may cause x2 to be discarded even if (x2-x1)/step is "almost" an integer. If x2*sign(step)<x1*sign(step), the result is an empty matrix.

If x1 or step is complex, a complex vector is produced, with the expected contents. The following algorithm is used to generate each element:

z = x1
while real((x2 - z) * conj(step)) >= 0
    append z to the result
    z = z + step
end

Values are added until they go beyond the projection of x2 onto the straight line defined by x1 and direction step. If x2-x1 and step are orthogonal, it is attempted to produce an infinite number of elements, which will obviously trigger an out of memory error. This is similar to having a null step in the real case.

Note that the default step value is always 1 for consistency with real values. Choosing for instance sign(x2-x1) would have made the generation of lists of indices more difficult. Hence for a vector of purely imaginary numbers, always specify a step.

colon(x1,x2) is equivalent to x1:x2, and colon(x1,step,x2) to x1:step:x2. It can be used to redefine this operator for objects.

The colon character is also used to separate the alternatives of a conditional expression b?x:y.

Example

2:5
  2 3 4 5
2:5.3
  2 3 4 5
3:3
  3
3:2
  []
2:2:8
  2 4 6 8
5:-1:2
  5 4 3 2
0:1j:10j
  0 1j 2j 3j 4j 5j 6j 7j 8j 9j 10j
1:1+1j:5+4j
  1 2+1j 3+2j 4+3j 5+4j
0:1+1j:5
  0 1+1j 2+2j 3+3j 4+4j 5+5j

See also

repmat, operator ?

Operator @

Function reference or anonymous function.

Syntax

@fun
@(arguments) expression

Description

@fun gives a reference to function fun which can be used wherever an inline function can. Its main use is as the argument of functions like feval or integral, but it may also be stored in lists, cell arrays, or structures. A reference cannot be cast to a number (unlike characters or logical values), nor can it be stored in a numeric array. The function reference of an operator must use its function name, such as @plus.

Anonymous functions are an alternative, more compact syntax for inline functions. In @(args) expr, args is a list of input arguments and expr is an expression which contains two kinds of variables:

  • input arguments, provided when the anonymous expression is executed;
  • captured variables (all variables which do not appear in the list of input arguments), which have the value of variables of the same name existing when and where the anonymous function is created. These values are fixed.

If the top-level element of the anonymous function is itself a function, multiple output arguments can be specified for the call of the anonymous function, as if a direct call was performed. Anonymous functions which do not return any output are also valid.

Anonymous functions may not have input arguments with default values (@(x=2)x+5 is invalid).

Anonymous functions are a convenient way to provide the glue between functions like fzero and ode45 and the function they accept as argument. Additional parameters can be passed directly in the anonymous function with captured variables, instead of being supplied as additional arguments; the code becomes clearer.

Examples

Function reference:

integral(@sin, 0, pi)
  2

Anonymous function:

a = 2;
fun = @(x) sin(a * x);
fun(3)
  -0.2794
integral(fun, 0, 2)
  0.8268

Without anonymous function, parameter a should be passed as an additional argument after all the input arguments defined for integral, including those which are optional when parameters are missing:

integral(inline('sin(a * x)', 'x', 'a'), 0, 2, [], false, a)
  0.8268

Anonymous functions are actually stored as inline functions with all captured variables:

dumpvar(fun)
  inline('function y=f(a,x);y=sin(a*x);',2)

Anonymous function with multiple output arguments:

fun = @(A) size(A);
s = fun(ones(2,3))
  s =
    2 3
(m, n) = fun(ones(2,3))
  m =
    2
  n =
    3

See also

fun2str, str2fun, inline, feval, apply