TExcelTAD
Home › MATLAB / Octave › Array Operations

Array Operations

MATLAB has two families of arithmetic operators. Matrix operators (*, /, \, ^) follow the rules of linear algebra. Array operators (.*, ./, .\, .^) work element by element. Mixing them up is the most common MATLAB bug, so this lesson is worth reading carefully.

Matrix operator Array (element-wise) operator
A * B A .* B Multiply
A / B A ./ B Divide
A \ B A .\ B Left divide
A ^ n A .^ n Power
A' A.' Transpose (' also conjugates complex numbers)

+ and - are always element-wise, so they have no dotted version.

Element-wise operations

With the dot operators, the two arrays must have the same size (or be compatible, see below), and each element is combined with its partner:

matlab · try it yourself
a = [1 2 3];
b = [4 5 6];
disp(a .* b)
disp(b ./ a)
disp(a .^ 2)
disp(2 .^ a)
    4   10   18
   4.0000   2.5000   2.0000
   1   4   9
   2   4   8

Matrix operations

* is the matrix product: each element of the result is the dot product of a row of the left matrix with a column of the right one. With vectors, a row times a column gives a scalar (the dot product) and a column times a row gives a full table (the outer product):

matlab · try it yourself
a = [1 2 3];
b = [4 5 6];
disp(a * b')       % 1x3 times 3x1 = 1x1
disp(a' * b)       % 3x1 times 1x3 = 3x3
try
    a * b
catch err
    disp('a * b fails: inner dimensions 3 and 1 do not match')
end
32
    4    5    6
    8   10   12
   12   15   18
a * b fails: inner dimensions 3 and 1 do not match

The error message is your hint that you probably wanted .*.

Squaring: ^ vs .^

A^2 means A*A, the matrix product of A with itself, and requires a square matrix. A.^2 squares each element:

matlab · try it yourself
A = [1 2; 3 4];
disp(A ^ 2)
disp(A .^ 2)
    7   10
   15   22
    1    4
    9   16

Tip: When you evaluate a formula on a vector of x-values, such as y = x² + 1/x, you almost always want the dotted operators: y = x.^2 + 1./x. Ask yourself "do I want linear algebra here?" If not, use the dot.

Operations with scalars

A scalar combines with every element of an array. For scalars, * and .* give the same result, and / with a scalar on the right works too. But dividing a scalar by an array needs ./:

matlab · try it yourself
v = [1 2 4];
disp(v * 3)
disp(v / 2)
disp(1 ./ v)
disp(v + 10)
    3    6   12
   0.5000   1.0000   2.0000
   1.0000   0.5000   0.2500
   11   12   14

Implicit expansion

When one array is a row and the other a column (or one dimension has size 1), element-wise operators expand the size-1 dimension to match. This lets you build tables without loops or repmat:

matlab · try it yourself
rows = (1:4)';      % column 4x1
cols = 1:3;         % row 1x3
disp(rows .* cols)  % 4x3 multiplication table

data = [10 20; 30 40; 50 60];
colMeans = mean(data);          % 1x2
disp(data - colMeans)           % subtract each column's mean
    1    2    3
    2    4    6
    3    6    9
    4    8   12
  -20  -20
    0    0
   20   20

Implicit expansion has existed in Octave for a long time and in MATLAB since R2016b. In older MATLAB versions you'd write bsxfun(@minus, data, colMeans).

A practical example

Evaluate the function f(x) = x·e^(−x) at several points and find where it peaks:

matlab · try it yourself
x = 0:0.5:4;
y = x .* exp(-x);
[ymax, k] = max(y);
fprintf('%4.1f  %.4f\n', [x; y]);
fprintf('Largest value %.4f at x = %.1f\n', ymax, x(k));
 0.0  0.0000
 0.5  0.3033
 1.0  0.3679
 1.5  0.3347
 2.0  0.2707
 2.5  0.2052
 3.0  0.1494
 3.5  0.1057
 4.0  0.0733
Largest value 0.3679 at x = 1.0

With x * exp(-x) instead of .*, MATLAB would complain that the inner matrix dimensions don't agree.

Test yourself

What is [1 2 3] .* [2 2 2]?

  1. 12
  2. [2 4 6]
  3. An error
  4. [3 4 5]

For A = [1 2; 3 4], what does A^2 compute?

  1. Each element squared
  2. The matrix product A*A
  3. A transposed
  4. An error

What is the size of (1:3)' + (1:5)?

  1. 1x5
  2. 3x1
  3. 3x5
  4. An error