Now that we know how to handle matrices and vectors in MATLAB, we will look at how to perform operations on them.
If two vectors or matrices are of the correct size, then they may be added, subtracted or multiplied using the standard operators +
, -
and *
.
For example, enter the following vectors and matrices:
u=[1 2];
v=[3; 4];
A=[1 2; 3 4];
B=[5 6; 7 8];
The following are valid operations:
u*v
v*u
A+B
A-B
A*B
A*v
u*A
(A+B)*v
but
A*u
is not. Try entering the above operations in the command line.
What do the above operations represent and what is the result: a matrix; a vector; or a scalar?
u*v
– inner product (dot product) – scalar (1 x 1).v*u
– vector outer product – matrix (2 x 2).A+B
– matrix addition – matrix (2 x 2).A-B
– matrix subtraction – matrix (2 x 2).A*B
– matrix multiplication – matrix (2 x 2).A*v
– right multiplication of a matrix by a vector – vector (2 x 1).u*A
– left multiplication of a matrix by a vector – vector (1 x 2).(A+B)*v
– matrix addition and right multiplication – vector (2 x 1).Why is A*u
not a valid operation?
The product A*u
would represent right multiplication of a 2 x 2 matrix by a 1 x 2 vector, which is an incompatible matrix multiplication.
The appropriate dimensions must agree to use the *
operator.
Similarly, v*A
would also fail.
Attempting an incompatible matrix multiplication will result in an error like this:
>> A*u
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix.
Often we wish to perform operations on elements of a vector or a matrix.
We may, for example, have a vector of numbers and wish to calculate their squares.
This is done using the ‘dot’ command, '.'
For example, enter the following vectors:
u=[1 2 3 4];
v=[5 6 7 8];
To calculate the square of all elements of u
use the command:
u.^2
To multiply each element of u
by the corresponding element of v
use:
u.*v
This also works with matrices, for example:
A.^2
This only works if the sizes of the vectors/matrices are the same. It is good practice to clear your variables once you have finished using them.
It can be tedious to input vectors by specifying all the entries manually; the following are two methods to define vectors more quickly.
A row vector x
consisting of integers from 1 to 10 inclusive may be generated by typing:
x=1:10
Furthermore, a row vector x
consisting of every odd integer from 1 to 9 inclusive may be generated by typing
x=1:2:9
The central variable indicates the step size, so
x=10:-1:1
would yield a row vector consisting of the integers from 1 to 10 inclusive, but in descending order.
A vector y
consisting of 100 points equally spaced between 5 and 15 may be generated by typing
y=linspace(5,15,100)
look up the linspace
command in the MATLAB help system.
MATLAB has inbuilt commands that can be used to create some simple matrices, which can be adapted using your own code to construct matrices of interest to you. You will see examples of this in later units.
The 3 x 4 matrix with all entries equal to 0 may be produced using the command
zeros(3,4)
The 5 x 2 matrix with all entries equal to 1 may be produced using the command
ones(5,2)
The 4 x 6 matrix with diagonal entries equal to 1 and all other entries equal to 0 is given by
eye(4,6)
The 2 x 1 matrix (that is, a column vector of length 2) with entries that are distributed randomly with a uniform distribution on the interval $[0,1]$
is given by
rand(2,1)
The 1 x 3 matrix (that is, a row vector of length 3) with entries uniformly distributed with mean 0 and standard deviation 1 is given by
randn(1,3)
If the matrix required with any of the commands in this section is square, then the second index is not required, as for example with
ones(4)
rand(2)