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 functions such as feval which take the name of a function as an argument.

Here is the correspondence between operators and functions:

a;bvertcat(a,b)
a,bhorzcat(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(predicate)
v(predicate,:)
v(:,predicate)
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 a matrix or list variable.

Matrices: In LME, any numerical object is considered as a two-dimension matrix. Therefore, two indices are required to specify a single element; the first index specifies the row, and the second the column. In some circumstances, however, it is convenient to consider a matrix as a vector, be it a column vector, a row vector, or even a matrix whose elements are indexed row-wise. For this way of handling matrices, a single index is specified. In all indexing operations, several indices can be specified simultaneously to extract more than one element. A single colon means all the elements in that dimension. The first valid value of an index is always 1. The matrix whose elements are extracted must be the content of a variable (an expression like [1,2;3,4](1,1) is invalid).

Instead of indices, the elements to be extracted can be selected by the true values in an array of the same size as the variable, or in a vector of the adequate size if some elements of a vector or some rows or columns of a matrix are extracted. Calculating a boolean expression based on the variable itself used as a whole is the easiest way to get such a logical array. LME knows whether to consider the expression between the parenthesis as an index or a predicate by looking at the logical flag of the object, which is set by all comparison operators and test functions and reset by arithmetic operators and mathematical functions.

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:

When indices are larger than the dimensions of the variable, the variable is expanded; new elements are set to 0.

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. Predicate are not supported with lists.

Examples

(1+2)*3
  9
a = [1,2,3;4,5,6;7,8,9];
a(2,3)
  6
a(2,:)
  4 5 6
a(:,3)
  3
  6
  9
a(1:2,[1,3])
  1 3
  4 6
a(3:5)
  3
  4
  5
a(:)
  1
  2
  3
  4
  5
  6
  7
  8
  9
a(a>=8)
  8
  9
a(sum(a,2) > 8, :)
  4 5 6
  7 8 9
a(1,5) = 99
  a =
    1 2 3 0 99
    4 5 6 0 0
    7 8 9 0 0
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]}
a(4) = {}
  a =
    {1,[3,9],'abc',[3,9]}

See also

Operator {}, end, reshape, variable assignment, operator []

Operator []

Brackets.

Syntax

[matrix_elements]

Description

A pair of brackets is used to define a matrix given by its elements or by submatrices. The operator , (or spaces) is used to separate elements on the same row, and the operator ; 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.

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

See also

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

Operator {}

Braces.

Syntax

{list_elements}
v{index}
v{index} = expr

Description

A pair of braces is used to define a list given by its elements. The operator , (or spaces) is used to separate elements. Since the space is considered as a separator when it is in the direct scope of braces, 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 whose result is of any type and size.

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.

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}

See also

operator ,, operator [], operator (), operator ;

Operator +

Addition.

Syntax

x + y
M1 + M2
M + x
+x
+M

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 numerical encoding. The prefix + is actually a synonym of double.

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
-x
-M

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.

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 *

Multiplication.

Syntax

x * y
M1 * M2
M * x

Description

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

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

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.

Example

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

See also

operator *, operator ./, operator .^

Operator /

Matrix right division.

Syntax

x / y
M1 / M2
M / x

Description

x/y 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).

Example

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

See also

operator \, inv, deconv

Operator ./

Scalar right division.

Syntax

x ./ y
M1 ./ M2
M ./ x
x ./ M

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.

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

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.

Examples

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

See also

operator /, inv

Operator .\

Scalar left division.

Syntax

M1 .\ M2
M1 .\ x

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.

Example

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

See also

operator \, operator ./

Operator ^

Matrix power.

Syntax

x ^ y
M ^ k
x ^ M

Description

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

Other cases yield an error.

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

See also

operator .^, expm

Operator .^

Scalar power.

Syntax

M1 .^ M2
x .^ M
M .^ x

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.

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

M2 = M1'

Description

M1' is the transpose of the real matrix M1, i.e. columns and rows are permuted. If M1 is complex, the result is the complex conjugate transpose of M1.

Examples

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

See also

operator .', conj

Operator .'

Transpose.

Syntax

M2 = M1'

Description

M1.' is the transpose of the matrix M1, i.e. columns and rows are permuted. M1 can be real or complex.

Example

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

See also

operator ', fliplr, flipud, rot90

Operator ==

Equality.

Syntax

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 a matrix, the comparison is performed element-wise and the result has the same size.

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. Contrary to the equality operator, same returns a single boolean even if its operands are arrays. In addition, numbers, logical values and strings are not considered as equivalent.

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

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 a matrix, the comparison is performed element-wise and the result has the same size.

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. Contrary to the inequality operator, ~== yields a single boolean even if its operands are arrays. In addition, numbers, logical values and strings are not considered as equivalent.

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

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 a matrix, the comparison is performed element-wise and the result has the same size.

Example

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

See also

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

Operator >

Greater than.

Syntax

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 a matrix, the comparison is performed element-wise and the result has the same size.

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

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 a matrix, the comparison is performed element-wise and the result has the same size.

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

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 a matrix, the comparison is performed element-wise and the result has the same size.

Example

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

See also

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

Operator ~

Not.

Syntax

~b

Description

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

Examples

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

See also

operator ~=, bitcmp

Operator &

And.

Syntax

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.

Example

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

See also

operator |, xor, 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

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.

Example

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

See also

operator &, xor, 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]

Description

Between brackets, the comma is used to separate elements on the same row in a matrix. Elements can be scalars, vector or matrices; their number of rows must be the same, unless one of them is an empty matrix.

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

Example

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

See also

operator [], operator ;, join

Operator ;

Vertical matrix concatenation.

Syntax

[M1; M2]

Description

Between brackets, the semicolon is used to separate rows in a matrix. Rows can be scalars, vector or matrices; their number of columns must be the same, unless one of them is an empty matrix.

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

Example

[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 :

Range.

Syntax

x1:x2
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 content. The following algorithm is used to generate each element:

z = x1
while real((z - x1)/(x2 - x1)) <= 1
    add z to the vector
    z = z + step
end

This algorithm is robust enough to stop even if x2 is not on the complex straight line defined by x1 and 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.

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 @

Function reference.

Syntax

@fun

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 quad, but it may also be stored in lists or structures. A reference cannot be cast to a double (unlike characters or logical values), nor can it be stored in an array.

Example

quad(@sin, 0, pi)
  2

See also

inline, feval, apply


Copyright 1998-2001, Calerga.

All rights reserved.