Vectors and matrices

The following walkthrough exercises contain a number of activities designed to teach you the basics of using MATLAB for working with vectors and matrices.

Basic vector and matrix manipulation

You have already learned many basic matrix manipulation commands in the first unit. This walkthrough provides a quick review of this material with a few new commands added.

Walkthrough

Let

x=[1,2,3]
y=[2,4,6]'
z=[2;4;6]

Enter the following commands on the command line and make sure you understand what they do. If you are unsure use the MATLAB help files.

x*y
y*x
y*z
y.*z
sum(x)
prod(y)
Question

Do all of the operations work? If one doesn’t then why not.

Expand for solution
Solution

y*z will not work as this is multiplying two column vectors together.

Now let

A=[1,2,3;4,5,6;1,3,2]

Enter the following commands on the command line and make sure you understand what they do. If you are unsure use the MATLAB help files.

B=A'
A*y
A*B
det(A)
inv(B)
B*inv(B)
trace(A)
A(1,2)
A(:,2)
A(1,:)
A(2,2:3)
A([2,1,3],:)
A^2
A.^2
sum(A,1)
prod(A,2)
C=[A,z]
ones(5,1)
ones(5)
zeros(1,5)

Now clear the workspace

clear

Size of vectors and matrices

You also learned lots of commands for finding the size of vectors and matrices.

Walkthrough

Let

A=randn(512);
b=randn(512,1);

Enter the following commands on the command line and make sure you understand what they do. If you are unsure, use the MATLAB help files.

length(A)
size(A)
size(b)
b(end)
b(end-5:end)

Now clear the workspace

clear

Parts of vectors and matrices

We often wish to look at certain parts of matrices: the diagonal entries, for example. The following will introduce some commands that will allow you to select specific entries.

Walkthrough

Let

A= randn(10);

Enter the following commands on the command line and use spy to visualise the non-zero entries of the resulting matrices. What do you see?

Make sure you understand what all the commands do. If you are unsure, use the MATLAB help files.

triu(A)
tril(A)
diag(A)
diag(A,2)
diag(diag(A))

Now clear the workspace

clear