MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB
MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB
Preliminaries
The MATLAB Desktop Display
The MATLAB default desktop consists of four windows: the command window, the Current Directory
Browser, the Workspace Browser, and the Command History.
The command window is where MATLAB commands are entered and executed.
The Current Directory Browser allows you to view MATLAB and other files and to perform file
operations such as opening and editing or searching for files.
The Workspace Browser allows you to view and make changes to the contents of the workspace.
The Command History allows you to view a log of all the commands that have been entered in the
command window. To repeat a previous command, just click on the command to highlight it and
then double-click to execute it. You can also recall an edit command directly from the command
window by using the arrow keys. From the command window, you can use the up arrow to recall
previous commands. The commands can then be edited using the left and right arrow keys. Press
the Enter key of your computer to execute the edited command.
Any of the MATLAB windows can be closed by clicking on the in the upper right corner of the
window. To detach a window from the MATLAB desktop, click on the arrow that is next to the x in
the upper right corner of the window.
Help Facility MATLAB includes a HELP facility that provides help on all MATLAB features.
doc: if you type doc in the command window the MATLABs help browser will open (alternatively
you can click on the Help button in the Home toolbar .)
help : if you know the exact name of a function, you can get help on it by typing help functioname.
For example, typing help help provides help on the function help itself.
lookfor: If you are looking for a function, use lookfor keyword to get a list of functions with the
string keyword in them. For example, typing lookfor identity matrix lists functions (there
are two of them) that create identity matrices.
If you have never used MATLAB before, we suggest you type demo at the MATLAB prompt. Click
on Getting Started with MATLAB and run the file. Then move on to the demo on Working in the
Development Environment and the demo on Working with Arrays.
c
2011
Stefania Tracogna, SoMSS, ASU
Matrices in MATLAB
Entering matrices in MATLAB is easy. For example, to enter the matrix
1
2
3
4
5
6
7
8
A=
9 10 11 12
13 14 15 16
type A=[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16]
or the matrix could be entered one row at a time:
A=[ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
Once a matrix has been entered, you can edit it. Here are some examples:
Input
Output
A(1,3) = 5
A =
C = A(2:3,2:4)
A(:,2:3)
A(4,:)
E = A([1,3],[2,4])
1
5
9
13
2
6
10
14
5
7
11
15
6
10
7
11
8
12
4
8
12
16
C =
Submatrix of A consisting of
all the elements in the second
and third columns
ans =
2
6
10
14
3
7
11
15
ans =
13
14
Fourth row of A
E =
2
10
4
12
15
16
Vectors
Vectors are special cases of matrices, with just one row or one column. They are entered the same way
as a matrix. For example
u = [1, 3, 9] produces a row vector
v = [1; 3; 9] produces a column vector.
Row vectors of equally spaced points can be generated with MATLABs : operation or using the
linspace command.
c
2011
Stefania Tracogna, SoMSS, ASU
Input
Output
x =
2
x = 2:6
x =
x = 1.2:0.2:2
1.2000
1.4000
1.6000
1.8000
2.0000
1.2000
1.4000
1.6000
1.8000
2.0000
x =
x = linspace(1.2,2,5)
Generating Matrices
We can also generate matrices by using the built-in MATLAB functions. For example, the command
B=rand(4)
generates a 4 4 matrix whose entries are random numbers between 0 and 1. Here is a list of the most
common built-in matrices:
rand(m,n)
eye(m,n)
zeros(m,n)
ones(m,n)
triu(A)
tril(A)
diag(v,k)
The first four commands above with a single argument, e.g. ones(m), produce a square matrix of
dimension m.
More special matrices:
There is also a set of built-in special matrices such as magic, hilb, pascal, toeplitz, and vander.
The matrix building commands can be used to generate block of partitioned matrices. Here is an
example:
Input
E=[eye(2),ones(2,3);zeros(2),[1:3; 3:-1:1]]
Output
E =
1
0
0
0
0
1
0
0
1
1
1
3
1
1
2
2
1
1
3
1
Exponentiation
Powers of matrices are easily generated. The matrix A5 is computed in MATLAB by typing A^5. We
can also perform operations element-wise by preceding the operand by a period.
For instance, if V=[1,2; 3,4], then
Input
Output
ans =
V^2
7
15
10
22
component-wise exponentiation
ans =
V.^2
1
9
4
16
1 0 0
2
Examples: If A = 0 1 0, u = 5 6 7 , and v = 3, then
0 0 1
4
1
0
C = [A; u] produces C =
0
5
0
1
0
6
0
0
, a 4 3 matrix
1
7
1
D = [A, v] produces D = 0
0
0
1
0
0
0
1
2
3, a 3 4 matrix.
4
Output
= []
A =
1
11
A(:,3:5) = []
A([1,3],:)
= []
2
12
3
13
4
14
5
15
deletes the 3rd through 5th
columns of A
A =
1
6
11
2
7
12
A =
c
2011
Stefania Tracogna, SoMSS, ASU
10
3 2 5 4
For example if A = 1 3 8 0, then
6 3 1 3
Input
Output
min(A)
ans =
max(A)
sum(A)
prod(A)
-6
-8
14
18
18
ans =
ans =
7
ans =
40
EXERCISES
Instructions
You will need to record the results of your MATLAB session to generate your lab report. Create a
directory (folder) on your computer to save your MATLAB work in. Then use the Current Directory
field in the desktop toolbar to change the directory to this folder. Now type
diary lab1.txt
followed by the Enter key. Now each computation you make in MATLAB will be save in your directory
in a text file named lab1.txt. When you have finished your MATLAB session you can turn off the
recording by typing diary off at the MATLAB prompt. You can then edit this file using your favorite
text editor (e.g. MS Word).
Lab Write-up: Now that your diary file is open, enter the command format compact (so that when
you print out your diary file it will not have unnecessary spaces), and the comment line
% MAT 343 MATLAB Assignment # 1
Put labels to mark the beginning of your work on each part of each question, so that your edited lab
write-up has the format
% Question 1
.
.
% Question 2 (a)
c
2011
Stefania Tracogna, SoMSS, ASU
C=
5
5
5
3
0
0
0
,
0
5
N = 0
0
c
2011
Stefania Tracogna, SoMSS, ASU
0
5
0
0
0 ,
5
3
P =
3
3
3
1
Q = 0
0
1
1
0
1
1 .
1
6
2 6 0 0
1 0
3 9 0 0
0 1
0 0 1 2
0 0
G=
0 0 3 4
0 0
1 0 0 0 5 5
0 1 0 0
5 3
5. Manipulate a matrix: Do the following operations on matrix G created above in Problem 4.
(a) Extract the first 4 4 submatrix from G and store it in the matrix H, that is, create a matrix
2 6 0 0
3 9 0 0
H=
0 0 1 2
0 0 3 4
by extracting the appropriate rows and columns from the matrix G.
(b) Replace G(5,5) with 4.
(c) What happens if you type G(:,:) and hit return? Do not include the output in your lab
report, but include a statement describing the output in words.
What happens if you type G(:) and hit return? Do not include the output in your lab report,
but include a statement describing the output in words.
(d) What do you get if you type G(7) and hit return? Can you explain how MATLAB got that
answer? Try G(16) to confirm your answer.
(e) What happens if you type G(12,1) and hit return?
(f) What happens if you type G(G>5) and hit return? Can you explain how MATLAB got that
answer? What happens if you type G(G>5) = 100 and hit return? Can you explain how
MATLAB got that answer?
(g) Delete the last row and the third column of the matrix G.
6. See the structure of a matrix: Create a 20 20 matrix with the command A = ones(20);
Now replace the 10 10 submatrix between rows 6:15 and columns 6:15 with zeros. See the
structure of the matrix (in terms of nonzero entries) with the command spy(A).
Set the 5 5 submatrices in the top right corner and bottom left corner to zeros and see the
structure again.
NOTE: Use semicolon to suppress the output for all the matrices in this problem. In your lab-write
up include the pictures obtained with the spy command. To include the pictures, open your diary
file using a word processor such as MS Word then, on the MATLAB figure, select Edit and Copy
Figure, and paste the picture into the Word file. Make sure you crop and resize the picture so
that it does not take up too much space.
7. Create a symmetric matrix: Create an upper triangular matrix with the following command:
A = diag(1:6) + diag(7:11, 1) + diag(12:15, 2)
Make sure you understand how this command works (see the on-line help on diag). Now use the
upper off-diagonal terms of A to make A a symmetric matrix with the following command:
A = A + triu(A,1)
c
2011
Stefania Tracogna, SoMSS, ASU
c
2011
Stefania Tracogna, SoMSS, ASU