Array operations

Arithmatic operations

Matrix operations

You can search documentation with the keywords arithmetic, operators, or name of each operators.

MATLAB does not have compound assignment operators, such as += and *=. One should use x = x + 1 rather than x += 1 or ++x.

Element-wise operations

Relational and logical operations

Relational operators

>> x = 1:3;
>> x > 2
ans =
     0     0     1
>> x >= 2
ans =
     0     1     1
>> x < 2
ans =
     1     0     0
>> x <= 2
ans =
     1     1     0
>> x == 2
ans =
     0     1     0
>> x ~= 2
ans =
     1     0     1

Note that all the above results are logical, not floating point (double) or integers.

Logical operators

>> a = [false false true true];
>> b = [false true false true];
>> a & b
ans =
     0     0     0     1
>> a | b
ans =
     0     1     1     1
>> ~a
ans =
     1     1     0     0

Negation and “not equal to” operators are ~ and ~=, not !.

Operators && and || are only for scalars, and short-circuit operators.

function shortcircuitexample()
disp('true || f(true)');
disp(true || f(true)); % short-circuiting

disp('false || f(true)');
disp(false || f(true));

disp('true && f(false)');
disp(true && f(false));

disp('false && f(false)');
disp(false && f(false)); % short-circuiting
end

function b = f(b)
disp('f() is called.');
end
>> shortcircuitexample
true || f(true)
     1
false || f(true)
f() is called.
     1
true && f(false)
f() is called.
     0
false && f(false)
     0

Scalar expansion

The scalar value is automatically expanded to array the same size as the other side of the operator, and operators are evaluated as element-wise sense.

>> [1 2; 3 4] + 1 % == [1 2; 3 4] + [1 1; 1 1]
ans =
     2     3
     4     5
>> x = rand(1, 5)
x =
      0.04644      0.93959      0.31257         0.34      0.44658
>> x > 0.5 % == x > [0.5 0.5 0.5 0.5 0.5]
ans =
     0     1     0     0     0
>> 2 * [1 2 3; 4 5 6] % == [2 2 2; 2 2 2] .* [1 2 3; 4 5 6]
ans =
     2     4     6
     8    10    12
>> [1 2 3; 4 5 6] / 2 % == [1 2 3; 4 5 6] ./ [2 2 2; 2 2 2]
ans =
          0.5            1          1.5
            2          2.5            3

Matrix / scalar works but scalar / Matrix not. Consider about 10 / [2; 5].

Operator precedence

Operators have precedence, almost the same with “normal math.”

All binary operators of equal precedence are evaluated left to right, even power.

>> 1 + 2^2 * 3^2 / 6 - 5 % == (1 + (((2^2) * (3^2)) / 6)) - 5
ans =
     2
>> [3^2^3, (3^2)^3, 3^(2^3)] % left to right!
ans =
         729         729        6561
  1. (Highest) ()
  2. .', .^, ', ^
  3. + (unary), - (unary), ~
  4. .*, ./, .\, *, /, \
  5. + (binary), - (binary)
  6. :
  7. <, <=, >, >=, ==, ~=
  8. &
  9. |
  10. &&
  11. (Lowest) ||

Appendix

What do you think of the below expressions? (Note that MATLAB does not have -- operator.)

>> 1 - 2
ans =
    -1
>> 1 -- 2
ans =
     3
>> 1 --- 2
ans =
    -1
>> [1 - 2]
ans =
    -1
>> [1 -2]
ans =
     1    -2
>> [1 --- 2]
ans =
     1    -2
>> [1 ---2]
ans =
     1    -2
>> [1 - - - 2]
ans =
    -1