Vous êtes sur la page 1sur 8

Applied Matrix Theory - Math 551

MATLAB Project 1
Created by Prof. Diego Maldonado and Prof. Virginia Naibo

The goal of this project is to get you started with the MATLAB environment. Basic com-
mands involving matrices are introduced. During the lab session, your lab instructor will
teach you the MATLAB code necessary to complete this assignment.

Due date: Friday September 3rd at the beginning of your lab 2 session. No late lab
assignment will be accepted.

What you have to submit: You must submit printouts of all images indicated in part (i)
of Task III and all written answers to the questions in items (e) through (m) in Task IV.

Honor pledge: “On my honor, as a student, I have neither given nor received unauthorized
aid on this academic work.”

Follow all the instructions carefully.

TASK I.

(a) Type a column vector v with components −0.1, 4, 0, −10, −3.2, 7, 25, 2, 0.5, using the
appropriate MATLAB syntax. Then type in the following commands and observe what
they do.

>> w=v’

>> v(1)

>> v(1);

>> v(2)

>> w(3)

>> v(end)

>> v(2:4)

>> v(3:end)
>> v(1:3:end)

>> v(end:-1:1)

>> v(end:-2:1)

>> length(v)

(b) Type in the matrix  


2 −1 0 1 9

 3 2 −8 3 −4 


 10 −21 13 21 4 

A=
 3 −3 −9 0 5 


 2 9 17 6 3 

 −11 5 19 22 1 
5 8 −90 −7 0
using the appropriate MATLAB syntax. Type in the following commands and observe
what they do.

>> size(A)

>> B=A’

>> A1=A(:,3)

>> A2=A(5,:)

>> A3=A(1:2,1:3)

>> A4=A(1:3:end, 1:2:end)

>> A5=A(end:-1:1, end:-1:1)

>> A6=A(end:-3:1, :)

>> C=A

>> C(:,4)=0

>> [A C]

>> [A; C]

2
TASK II.

(a) Create a folder named Lab1. Create two folders inside Lab1 under the names TaskII
and TaskIII, respectively.
(b) Download A.mat from your KState online account (Labs/Lab1) to the folder TaskII and
load it into your MATLAB session.
(c) Check the size of A.
(d) Create the matrix B formed by the first two columns of A. Save B in the folder TaskII
under the name B.mat.

Keywords: load, size, save.

Note: One way of getting help from MATLAB is by using the MATLAB Help Browser
in the toolbar. If you want to see what a particular command does, for instance the command
load, type

help load

TASK III. In this task you will apply the commands from Task I to matrices representing
images. Recall that a black and white (or monochromatic) digital image can be represented
by a matrix whose entries, called pixels after picture elements, correspond to the intensities
(gray levels) of the image at different points. This correspondence between matrices and
images will help you visualize basic operations with matrices.

(a) Download peppers.jpg from your KState online account (Labs/Lab1) to the folder
TaskIII.
(b) Read the image into your MATLAB session by naming A the matrix representing it.
Display the image.
(c) Determine the size of the matrix A.
Apply to the matrix A an appropriate command from Task I to obtain the following
images:
(d) Original image upside down, meaning, the reflection of the original image with respect
to its middle horizontal axis. Call its matrix A2. Save the new image and the matrix A2
under the names peppers2.jpg and A2.mat, respectively, in the folder TaskIII.
(e) Reflection of the original image with respect to its middle vertical axis. Call its matrix
A3. Save the new image and the matrix A3 under the names peppers3.jpg and A3.mat,
respectively, in the folder TaskIII.

3
(f) Upper right subimage of the original image of size 200 × 200. Call its matrix A4. Save the
new image and the matrix A4 under the names peppers4.jpg and A4.mat, respectively,
in the folder TaskIII.

(g) The image represented by the matrix obtained from A by deleting the even rows. Call the
new matrix A5. Save the new image and the matrix A5 under the names peppers5.jpg
and A5.mat, respectively, in the folder TaskIII.

(h) The image represented by the transpose of A. Call the new matrix A6. Save the new
image and the matrix A6 under the names peppers6.jpg and A6.mat, respectively, in
the folder TaskIII.

(i) Print out the original image and the images obtained in items (d) through (h). Write next
to each image its name and the command that you used to obtain the matrices A2, A3,
A4, A5, A6, respectively. Staple all the sheets in the order peppers.jpg, peppers2.jpg,
· · · , peppers6.jpg.

Keywords: imread, imshow, imwrite, size, save.

TASK IV: Matrix operations.

(a) Addition and subtraction of matrices


Given two m × n matrices A and B, the sum A + B and difference A − B matrices are
also of size m × n and they are defined as

(A ± B)ij = Aij ± Bij , i = 1, . . . , m, j = 1, . . . , n.

That is, addition and subtraction of matrices are done entry by entry. In Matlab,

>> A + B

and

>> A - B

(b) The product of a number and a matrix


Given a real number (that is, a scalar ) r ∈ R and an m × n matrix A, the product rA
is an m × n matrix defined as

(rA)ij = rAij , i = 1, . . . , m, j = 1, . . . , n.

In Matlab

4
>> r*A

(c) The product of a row vector and a column vector of the same length.
Let v be a row vector of length l and w a column vector also of length l. The product
v ∗ w is the scalar defined by
X l
v∗w = vj wj .
j=1

In Matlab

>> v*w

Again, notice that the product (row vector) ∗ (column vector) is a real number.

(d) The product of two matrices of the right size.


Given an m × n matrix A and an n × r matrix B, the product AB is an m × r matrix
defined as n
X
(AB)ij = Aik Bkj , i = 1, . . . , m; j = 1, . . . r.
k=1

In Matlab

>> A*B

Notice that the number of columns of the first factor must coincide with the number of
rows of the second factor. A formula for the sizes can be expressed as

(m × n) ∗ (n × r) = m × r.

If A = B we write A2 for A ∗ A. In Matlab,

>> A^2

(e) Relate the product of matrices in (d) with the product of row and column vectors in (c).

(f) Is the product of matrices a commutative operation?

(g) By definition, the n × n identity matrix In has ones along its main diagonal and zeros
everywhere else. For instance, for n = 5,

5
>> I_5=eye(5)

I_5 =

1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

What happens if you left- or right-multiply a 5 × 5 matrix by I5 ?


(h) Derive a formula for A(B + C), where A, B, and C are matrices of the appropriate size.
(i) Derive a formula for the transpose of A + B, A − B and P Q, in terms of AT , B T , P T
and QT , where A, B, P , and Q are matrices of the appropriate size.
(j) Consider the matrices

>> H =[1 4 5; 6 7 8; 4 -2 -3]


H =

1 4 5
6 7 8
4 -2 -3

>> Q=[0 0 0; 0 0 0; 0 0 -1]


Q =

0 0 0
0 0 0
0 0 -1

>> R=[0 1 0; 0 0 0; 0 0 0]
R =

0 1 0
0 0 0
0 0 0

>> P=[1 0 0; 0 0 1; 0 1 0]
P =

1 0 0

6
0 0 1
0 1 0

Analyze the action of P , Q, and R on H. That is, look at the products QH, HQ, RH,
HR, P H, and HP . What do the matrices P , Q, and R do to H?

(k) Create the matrix J as

>> J=ones(5)

What do the following commands do?

>> tril(J)

>> tril(J,1)

>> tril(J,-1)

>> tril(J,2)

>> triu(J)

>> triu(J,2)

(l) Indicate how to use the commands round and rand to generate random binary matrices
of size 6 × 7.

(m) What do the following commands do?

>> zeros(3,4)

>> ones(2,5)

>> rand(3,3)

>> diag([-3 4 5 1 5 ])

>> diag([-3 4 5 1 5 ],1)

>> diag([-3 4 5 1 5 ],-2)

>> toeplitz([-1 3 2 6])

>> hankel([-1 3 2 6])

7
In just one Matlab line, indicate how to create a 50 × 50 tri-diagonal matrix T of the
form
−8 0 · · · 0
 
2
.
 5 2 −8 . . 0 
 
T =  ... .. .. .. . 
. .. 

 . . 
 0 ··· 5 2 −8 
0 ··· 0 5 2

Vous aimerez peut-être aussi