MATLAB Lesson 6 - Matrices

Matrix arithmetic

Addition + and subtraction - are defined for matrices of the same dimensions, and work elementwise.

Multiplication of a matrix by a scalar is also defined elementwise, just as for vectors.

Create a 3 by 2 matrix A, the calculate B = -2A and C = 2A + B.

A is a 3 by 2 matrix.

B is a 3 by 2 matrix with each element equal to -2 times the corresponding element of A.

The result C is the 3 by 2 matrix with each element equal to 0.

>>  A = [1 2; 3 4; 5 6]
>>  B = -2*A
>>  C = 2*A + B

Matrix multiplication

In MATLAB the multiplication operator * represents matrix multiplication.

If A and B are not scalars, then A*B is only defined if the number of columns in A is equal to the number of rows in B. If A is an m by n matrix and B is an n by p matrix then C = A*B is an m by p matrix.

Create a 3 by 2 matrix A and a 2 by 2 matrix B and their product C = AB.

A is a 3 by 2 matrix, B is a 2 by 2 matrix, so the number of columns in A = 2 = number of rows in B.

The result C is a 3 by 2 matrix.

>>  A = [1 2; 3 4; 5 6]
>>  B = [5 6; 7 8]
>>  C = A*B

If you are unsure of the dimensions of a matrix, use the size command.

Calculate D = BA for the matrices A and B in the previous example.

This produces
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Use the size command to check the dimensions.

The number of columns in B is 2, which is not equal to the number of rows (3) in A.

>>  D = B*A
>>  size(B)
>>  size(A)




This example illustrates the fact that matrix multiplication is not commutative, that is AB is not necessarily equal to BA.

Connections between vectors and matrices

A row vector with n elements is equivalent to a 1 by n matrix.

A column vector with m elements is equivalent to a m by 1 matrix.

Thus b = A*x for an m by n matrix A implies that x is a column vector with n elements (n by 1 matrix) and b is a column vector with m elements (m by 1 matrix).

Let u be a column vector with 3 elements. Calculate A = u*u' and B = u'*u.

This produces a 3 by 1 matrix (column vector).

As u is a 3 by 1 matrix its transpose u' is a 1 by 3 matrix, so A is a 3 by 3 matrix.

As the transpose u' is a 1 by 3 matrix and u is a 3 by 1 matrix, so B is a 1 by 1 matrix, that is a scalar.

>>  u = [1; 2; 3]
>>  A = u*u'
>>  B = u'*u


For column vectors a and b, a'*b equals the dot product of a and b.

Matrix powers

Just as * represents matrix multiplication, ^ represents the multiplication of matrices together. Thus A^2 = A*A and A^3 = A*A*A, etc, are only defined for square matrices A.

Create a 2 by 2 matrix A and calculate A^2 and A^3.

A is a 2 by 2 matrix

B = A*A

C = A*A*A

>>  A = [1 2; 3 4]
>>  B = A^2
>>  C = A^3

 

Elementwise operations

MATLAB provides the operators .* for element by element multiplication, ./ for element by element division and .^ for element by element powers.

This works is the same way as with vectors.

For a 2 by 3 matrix A, what do B=1./A and C=1/A produce?.

Define a 2 by 3 matrix A.

This produces the 2 by 3 array with B(i,j) = 1/A(i,j).

This produces
??? Error using ==> mrdivide Matrix dimensions must agree.

>>  A = [1 2 3; 4 5 6]
>>  B = 1./A
>>  C = 1/A


 

Warnings

Self-test Exercise

For the matrix A = [3 2; 1 2] calculate B = A^2 - 5A + 4I where I is the identity matrix of the appropriate size.

Answer:
A = [3 2; 1 2]
B = A^2 - 5*A + 4*eye(2)

Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

Addition and scalar multiplication works for matrices, just as for vectors.

Multiplication * is matrix multiplication, just as ^ represents matrix powers.

Element by element operators are: .* multiplication, ./ division and .^ powers.