Matrices

# 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 often-used 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: B = column 1 = row

## 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]


## 4.5. Addressing an array review

• How did we access the third value of an array?
A = [1, 4, 2, 5, 3, 6]

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


## 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

## 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)?

## 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)?

# 5. Questions

## 5.1. Syntax I

What will happen when you enter the following commands?

clear;
M(2,2) = 1.0;
M

clear;
M(2,2) = 4;
b = M(2,2) + M(1,2)


## 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]

clear;
M(0,1) = 1;
M(1,1) = M(0,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 M that looks like this:

 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 M that looks like this:

 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

• Original program

for i = [1:3]

for j = [1:2]
B(i,j) = i;
end

end

• Modify the program to the left so that it creates a matrix B with values of
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

• Modify the program to the left so that it creates a matrix B with values of
1 2 3
1 2 3
1 2 3

• Extra credit: Modify the program to the left so that it creates a matrix B with values of
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 semi-colon 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


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


## 6.2. Long-hand version of nested for loop

• Re-write the nested for loop program given below in long-hand form in two steps. First re-write the inner for loop in long-hand and leave the outer loop. Then re-write the outer part in long-hand form.
• What will the matrix B look like after you run the program?
• Original program

for i = [1:3]

for j = [1:2]
B(i,j) = 1.0
end

end

• Step 1: Fill in the space below with the long-hand version of the inner for loop.
• Step 2: Fill in the space below with the long-hand version of what you wrote for step 2.

# 7. Video

## 7.1. Video

(Note - the video is available in HD - go to full screen mode if fonts are too small)

## 7.2. Follow-up Questions

After watching the video (an pre-requisite 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.