TExcelTAD
Home › MATLAB / Octave › Matrices

Matrices

A matrix is a two-dimensional array of numbers arranged in rows and columns. In MATLAB everything numeric is a matrix: a scalar is 1-by-1 and a vector is 1-by-n or n-by-1. You type a matrix row by row, with spaces (or commas) between elements and semicolons between rows.

matlab · try it yourself
A = [1 2 3; 4 5 6]
[r, c] = size(A)
A =

   1   2   3
   4   5   6

r = 2
c = 3

You can also press Enter instead of typing ; when entering a matrix over several lines, which is easier to read:

matlab · try it yourself
grades = [78 85 90
          62 70 75
          88 92 95];
disp(grades)
disp(size(grades))
   78   85   90
   62   70   75
   88   92   95
   3   3

Every row must have the same number of elements: [1 2; 3] is an error.

Functions work column by column

Most functions that reduce data (sum, mean, max, min, prod, …) operate on each column of a matrix and return a row vector. Pass a dimension argument to work along rows instead: dimension 1 runs down the rows (one result per column), dimension 2 runs across the columns (one result per row).

matlab · try it yourself
grades = [78 85 90; 62 70 75; 88 92 95];
disp(sum(grades))          % per column (each test)
disp(sum(grades, 2))       % per row (each student)
disp(mean(grades, 2))
disp(max(grades(:)))       % (:) flattens to one column
disp(sum(grades, 'all'))
   228   247   260
   253
   207
   275
   84.333
   69.000
   91.667
95
735

grades(:) turns any matrix into a single column, so max(grades(:)) finds the largest value overall. sum(A, 'all') does the same for sums.

Transpose and matrix arithmetic

The transpose A' swaps rows and columns. Adding, subtracting and multiplying by a scalar work element by element. The * operator between two matrices is the matrix product from linear algebra:

matlab · try it yourself
A = [1 2; 3 4];
B = [0 1; 1 0];
disp(A')
disp(A + 10)
disp(A * B)      % matrix product: swaps the columns of A
disp(B * A)      % swaps the rows: order matters!
   1   3
   2   4
   11   12
   13   14
   2   1
   4   3
   3   4
   1   2

For A * B the number of columns of A must equal the number of rows of B. If you want to multiply matching elements instead, use .* (covered in Array Operations).

Special matrices

MATLAB has functions that build common matrices. A few useful ones (the next lesson covers zeros, ones and eye in detail):

matlab · try it yourself
M = magic(4)          % rows, columns and diagonals all sum to the same value
disp(sum(M))
disp(sum(M, 2)')
disp(trace(M))        % sum of the main diagonal
disp(diag(M)')        % the diagonal itself
M =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

   34   34   34   34
   34   34   34   34
34
   16   11    6    1

diag also works the other way round: given a vector, diag([1 2 3]) builds a diagonal matrix.

Empty matrices

[] is a 0-by-0 matrix. It's the natural starting point when you collect values in a loop, and many functions return it when there is nothing to return:

matlab · try it yourself
E = [];
disp(isempty(E))
disp(size(E))
E = [E 5];
E = [E 7];
disp(E)
disp(isempty(find([1 2 3] > 10)))
1
   0   0
   5   7
1

Test yourself

What does sum([1 2; 3 4]) return?

  1. 10
  2. [3 7]
  3. [4 6]
  4. [1 2 3 4]

How do you get the sum of each row of a matrix A?

  1. sum(A)
  2. sum(A, 2)
  3. sum(A')' only
  4. rowsum(A)

Which is true about A * B for matrices?

  1. It always multiplies matching elements
  2. It is the matrix product, and A*B usually differs from B*A
  3. It requires A and B to be the same size
  4. It is an error for non-square matrices