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.
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.
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.
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.
If you are unsure of the dimensions of a matrix, use the size command.
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.
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).
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.
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.
A is a 2 by 2 matrix
B = A*A
C = A*A*A
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.
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.
Warnings
- One of the most common mistakes in MATLAB is to get the dimensions wrong, so matrix multiplication or matrix powers are not defined. Remember the size command.
- Be very careful when using the division operators / and \ with matrices. Some more information will come in the module on linear systems.
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.