Matrices
From CDS 130
1. Objective
 To introduce the double index notation associated with matrices in a computer program.
 To introduce single index notation for extracting and assigning elements of a matrix.
2. Motivation
 Images are two dimensional. Arrays are one dimensional. We need programming structure that is two dimensional to more easily handle images.
 The matrix is an oftenused method for describing information.
 In order to be able to do computation on complex objects such as images, you need to understand other ways of structuring the numbers that correspond to the values that you see in the image.
3. Priming questions
 You have previously used matrices in algebra class. You may recall things like Cramer's rule and the transpose of a matrix. What was the motivation for using a matrix? Could you solve algebra problems without using a matrix?
4. Notes
4.1. Matrix motivation
 Suppose that you wanted to enter latitudes and longitudes into an array. Here are the latitudes and longitudes of three cities, Fairfax, Greenwich, and Hell:
A = [38.83, 77.30, 0.0, 0.0, 63.433, 10.9000]
Instead of writing this on a piece of paper:
38.83, 77.30, 0.0, 0.0, 63.433, 10.9000
and saying "the latitude of Greenwich is in element 3, the longitude is in element 4", most people would write this on a piece of paper like this:
38.8 77.3 0.0 0.0 63.433 10.9
and say "the latitude of Greenwich is in row 2, column 1, the longitude is in row 2, column 2"
4.2. Matrices in a spreadsheet
If you want to get a value from this matrix, you could type =Sheet1!B1 Sheet1 is the name of the matrix The coordinates are:

4.3. Creating a matrix approach I
 Like an array, a matrix is a container for a list of numbers and is a data structure.
 A matrix has "shape".
To create the matrix 1.1 2.2 3.3 4.4 5.5 6.6 use array notation to indicate columns (by separating numbers with commas) and indicate new rows with a semicolon: M = [1.1, 2.2; 3.3, 4.4 ; 5.5, 6.6] 
We describe a position in a matrix by its row and column. Rows are numbered starting at the top of a column. Columns are numbered starting from the left (think of a column like an architectural structure, for example [1].) 
4.4. Creating a matrix approach I example
What do you think will happen when you try to enter this:
M = [1.1, 2.2; 3.3 ; 5.5, 6.6]
Answer 

You will see an error message. You have not specified two values for the second row, and MATLAB does not assume that you meant the second row and second column value to be zero (as you may have expected). 
4.5. Addressing an array review
 How did we access the third value of an array?
A = [1, 4, 2, 5, 3, 6]
 Answer: type
A(3)
on the command line. The response should be
ans = 2
4.6. Addressing a matrix
Given this matrix,
B = 1 4 2 5 3 6
the third row and first column would be accessed using double index notation:
B(3,1)
If you entered this on the command line, the response should be
ans = 3
Alternative approach 

The third row and first column may also be accessed using single index notation. (This is not recommended, but is useful to know because someday you may address a matrix with a single index and get an unexpected answer.) If you enter B(3) the response will be ans = 3 If you enter B(4) the response will be ans = 4 If you enter B(5) the response will be ans = 5 MATLAB interprets 1.1 5.5 2.2 6.6 3.3 7.7 4.4 8.8 and you enter C(5) the response will be ans = 5.5 and if you entered C(1,2) the response will be ans = 5.5 
4.7. Creating a matrix approach II
Suppose you entered clear; M(10,10) = 10.7 what do you expect for these elements M(1,1) M(2,2) ... M(11,11) ... 

4.8. Addressing a matrix with single index
If
B = [1, 4 ; 2, 5 ; 3, 6]
do you expect this to make sense?
B(4)
 It turns out that values in a matrix can be accessed using this type of notation (single index).
 The interpretation is "the fourth element of B".
 To find out its value, start counting elements by working your way down the rows of the first column.
B = 1 4 2 5 3 6
 1st element in B = 1
 2nd element in B = 2
 3rd element in B = 3
 4th element in B = 4
Note 

The convention used in MATLAB that the single index element value is determined by counting down the first column is called column major ordering. 
4.9. Addressing a matrix with single index
 Would you expect this to make sense?
B(4)
 It turns out that values in a matrix can be accessed using this type of notation.
 The interpretation is "the fourth element of B".
 To find out its value, start counting elements by working your way down the rows.
B = 1 4 2 5 3 6
 1st value in B = 1
 2nd value in B = 2
 3rd value in B = 3
 4th value in B = 4
4.10. Addressing a matrix examples
B = 1 4 2 5 3 6
 What is B(1,1) + B(2,2)?
 What is B(4) + B(5)?
Answer 


4.11. Addressing a matrix example
C = [2.1,3.1 ; 4.1, 4.2 ; 5.1, 5.2]
 What is C(1,1) + C(2,2)?
 What is C(4) + C(5)?
Answer 

C = 2.1000 3.1000 4.1000 4.2000 5.1000 5.2000

5. Questions
5.1. Syntax I
What will happen when you enter the following commands?
clear; M(2,2) = 1.0; M
Answer 

The first command assigns the value of 1.0 to the second row and second column. The elements M(1,1), M(1,2), and M(2,1) are assumed to be zero and 0 0 0 1 is displayed. 
clear; M(2,2) = 4; b = M(2,2) + M(1,2)
Answer 

b = 4 is displayed. Even though M(1,2) was never assigned explicitly, it was assumed to be zero. 
5.2. Syntax II
What is wrong with each of the following statements?
clear; M = [1.1; 2.2, 3.3 ; 5.5, 6.6]
Answer 

A matrix must have equal number of columns in each row. The first row only has one column while the second and third rows are given two columns. 
clear; M(0,1) = 1; M(1,1) = M(0,1);
Answer 

The statement M(0,1) is an error because a matrix must have index values that are integers starting at 1. 
5.3. Syntax III
Write your answers out below. Note that there are multiple ways of creating each matrix. Some approaches require more commands to be entered but less writing. Choose any method you like. Extra credit: Give two methods for creating each matrix.
Using approach 1 for creating a matrix covered in Matrices, create a matrix named 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

Using approach 1 for creating a matrix covered in Matrices, create a matrix named 5 4 3 4 4 3 3 4 3 2 4 3 1 4 3

5.4. Addressing a Matrix
If you entered on the command line
C = [1.0, 2.0, 3.1 ; 4.2, 3.3, 5.5 ; 1.1, 12.0, 13.0]
what do you expect when you type
C(1) + C(2)
and
C(1,2) + C(2,2)
and
C(3,3) + C(3,3)
5.5. Addressing an Array
Explain why if you typed
A = [1, 2, 3, 4]
then the command
A(2,2)
does not make sense (an error mesage is given).
5.6. Experiment I
Create a matrix by entering
M(2,2) = 10.0
on the command line.
 What is the response?
 Why are the other values zero?
 Explain what happens when you type
M(2,1) = 13.0
5.7. Experiment II
Enter the following commands (note that there is not a semicolon after the 12)
clear; M = [0, 0 ; 0, 0 ; 0, 1]; for i = [1:6] M(i) = 12 end
and explain what happens and explain why it happens.
Enter the following commands and explain what happens and why it happens (note that the matrix is named MM
and not M
).
for i = [1:3] for j = [1:2] MM(i,j) = 12 end end
5.8. Creating a matrix using a nested for loop
for i = [1:3] for j = [1:2] B(i,j) = i; end end 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3

1 2 3 1 2 3 1 2 3

1 2 3 2 4 6 3 6 9

5.9. Triple for loop
When the following is executed, describe what will be shown on the screen. (Note that the line counter = counter + 1
does not have a semicolon at the end.)
counter = 1; for i = [1:10] for j = [1:10] for k = [1:10] counter = counter + 1 end end end
6. Activities
6.1. Matrices
Using the "square brace notation" covered in Matrices, create a matrix named M
that looks like this:
1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2
Answer 

There are several ways of doing this. Method 1 clear; M = [1,1,2;1,1,2;1,1,2;1,1,2;1,1,2;1,1,2;1,1,2;1,1,2] Method 2 clear M = [1,1,2]; % This will stack the existing M on % top of itself. After entering, M will % have two rows and three columns. M = [M ; M]; % This will stack the existing M on % top of itself. After entering, M will % have four rows and three columns. M = [M ; M]; % This will stack the existing M on % top of itself. After entering, M will % have eight rows and three columns. M = [M ; M] Method 3 clear; M = [1,1,2;1,1,2;1,1,2;1,1,2]; M = [M ; M] Method 4 clear; M = [1,1,2;1,1,2;1,1,2;1,1,2]; M = [M ; 1,1,2;1,1,2;1,1,2;1,1,2] 
Using the "square brace notation" covered in Matrices, create a matrix named M
that looks like this:
5 4 3 4 4 3 3 4 3 2 4 3 1 4 3
Answer 

Method 1 clear; M = [5,4,3;4,4,3;3,4,3;2,4,3;1,4,3] Method 2 clear % This will create a matrix with % five rows and one column M = [5;4;3;2;1] % Create a matrix with five rows % and two columns. A = [4,3;4,3;4,3;4,3;4,3] % Place the matrix A to the right % of existing matrix M: M = [M,A] 
6.2. Longhand version of nested for loop
 Rewrite the nested for loop program given below in longhand form in two steps. First rewrite the inner for loop in longhand and leave the outer loop. Then rewrite the outer part in longhand form.
 What will the matrix
B
look like after you run the program?
for i = [1:3] for j = [1:2] B(i,j) = 1.0 end end 


7. Video
7.1. Video
(Note  the video is available in HD  go to full screen mode if fonts are too small)
7.2. Followup Questions
After watching the video (an prerequisite videos), you should be able to answer the following questions.
7.2.1.
How is an array related to a matrix?
7.2.2.
The video on arrays included three methods for creating an array. This video only showed two methods for creating a matrix. What method was omitted?
7.2.3.
Create the following array using two different methods.
M = 1 2 3 4 4 4 8 8 8
7.2.4.
Write a command that will change the value of 8 in the third row and third column to 9.
7.2.5.
With only one command, create a matrix that has 10 rows and 10 columns, all with values of zero.