CS 177 Lists and Matrices Week 8courses.cs.purdue.edu/_media/cs17700:spring15:rec-wk8.pdf · 2015....

35
1 CS 177 Lists and Matrices Week 8

Transcript of CS 177 Lists and Matrices Week 8courses.cs.purdue.edu/_media/cs17700:spring15:rec-wk8.pdf · 2015....

  • 1

    CS 177

    Lists and Matrices

    Week 8

  • Announcements Project 2 due on 7th March, 2015 at 11.59 pm

  • Table of Contents

    Lists

    Matrices

    Traversing a Matrix

    Construction of Matrices

    3

  • 1D arrayJust a list of numbers

  • 1D arrayYou can create an empty list

    - This breaks because my_array is an empty list so you can’t set an

    element of an empty list

  • 1D array So to add to an empty list you would have to append

  • 1D array You can also use a list comprehension to initialize an

    array

    This basically just creates an element 0 however many

    times the for loop runs

  • Matrices

    8

    Matrices in mathematics are simply arrays of numbers

    or variables arranged in both rows and columns.

    Each number or variable contained within the matrix

    can be uniquely identified by its position in the row and

    column.

  • Matrices

    9

    Each element of the matrix has a unique position determined by a row index i and a column index j.

    In the matrix below: the number 1, is defined to be in position 0,0 (located in row 0 and column 0)

    M = number of rows. N = number of columns.

    a0,0 a0,1 a0,2

    a1,0

    a2,0

  • Encoding a Matrix

    We will consider a List to encode a Matrix.

    Although the List below does not visibly look like the matrix on the

    right, it does contain the same data.

    10

  • Matrix - index

    11

  • Traversing a Matrix

    12

  • Example- Matrix

    13

  • Example- Matrix Contd..

    14

  • Construction of Matrices

    15

    In this case we create a 5 × 4 matrix populated

    with 0s.

  • Construction of Matrices

    16

    The loop is traversing the entire matrix, but it only

    assigns the value 1 if the row index i is equal to the

    column index j

  • Example 2 - Construction

    17

    What should be the output after the following loop:

  • Example 2 - Construction

    18

    What should be the output after the following loop:

  • Sum of Diagonal

    2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    0 1 2 3

    0

    1

    2

    3

    Exercise 1:

    Given a matrix A = [[2, 3, 1, 2], [6, 5, 7, 2], [2, 1, 1, 5], [3, 4, 5, 3]]

    Write a nested for loop that will calculate the sum of diagonal.

    19

  • Sum of Diagonal

    2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    0 1 2 3

    0

    1

    2

    3

    Exercise 1: Given a matrix A.

    Write a nested for loop that will calculate the sum of diagonal.

    20

    Sum = 0for i in range(len(A)):

    for j in range (len(A[0])):if (i==j):

    Sum = Sum + A[i][j]

  • Sum of Rows

    2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    0 1 2 3

    0

    1

    2

    3

    Exercise 2:

    Write a nested for loop that will calculate the sum of each row

    21

  • Sum of Rows

    2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    0 1 2 3

    0

    1

    2

    3

    Exercise 2:

    write a nested for loop that will calculate the sum of each row

    sumRow = 0for i in range(len(A)):

    for j in range (len(A[0])):sumRow = sumRow + A[i][j]

    print (sumRow)sumRow = 0

    22

  • Compare Codes

    Exercise 3:

    Are these two codes the same?

    sumRow = 0for i in range(len(A)):

    for j in range (len(A[0])):sumRow = sumRow + A[i][j]

    print (sumRow)sumRow = 0

    23

    for i in range(len(A)):sumRow = 0for j in range (len(A[0])):

    sumRow = sumRow + A[i][j]print (sumRow)

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

    1 2

  • Compare Codes

    Exercise 3:

    Are these two codes the same?

    sumRow = 0for i in range(len(A)):

    for j in range (len(A[0])):sumRow = sumRow + A[i][j]

    print (sumRow)sumRow = 0

    24

    for i in range(len(A)):sumRow = 0for j in range (len(A[0])):

    sumRow = sumRow + A[i][j]print (sumRow)

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

    1 2

    YES

  • Sum of Columns

    2 3 1 2 1

    6 5 7 2 1

    2 1 1 5 1

    3 4 5 3 1

    0 1 2 3

    0

    1

    2

    3

    Exercise 5:

    Write a nested for loop that will calculate the sum of each column

    25

    4

  • Sum of Columns

    2 3 1 2 1

    6 5 7 2 1

    2 1 1 5 1

    3 4 5 3 1

    0 1 2 3

    0

    1

    2

    3

    Exercise 5:

    Write a nested for loop that will calculate the sum of each column

    26

    4

    for i in range(len(A[0])):sumCol = 0for j in range (len(A)):

    sumCol = sumCol + A[j][i]print (sumCol)

  • Sum of two matrices

    A[0][0]+B[0][0]

    A[0][1]+B[0][1]

    …A[0][2]+B[0][2]

    A[3][0]+B[3][0]

    A[3][1]+B[3][1]

    …A[3][3]+B[3][3]

    = +C A B2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    5 5 5 5

    1 1 1 1

    3 3 3 3

    4 4 4 4

    27

    Exercise 6:

    Write a nested for loop that will calculate the sum of two matrices

  • Sum of two matrices

    A[0][0]+B[0][0]

    A[0][1]+B[0][1]

    …A[0][2]+B[0][2]

    A[3][0]+B[3][0]

    A[3][1]+B[3][1]

    …A[3][3]+B[3][3]

    = +C A B2 3 1 2

    6 5 7 2

    2 1 1 5

    3 4 5 3

    5 5 5 5

    1 1 1 1

    3 3 3 3

    4 4 4 4

    28

    Exercise 6:

    Write a nested for loop that will calculate the sum of two matrices

    for i in range(len(A)):for j in range (len(A[0])):

    C[i][j] = A[i][j]+B[i][j]

  • Max Element

    2 3 1 2 1

    6 5 7 2 8

    2 1 1 5 3

    3 4 5 3 1

    0 1 2 3

    0

    1

    2

    3

    Exercise 7 :

    Write a nested for loop that will find the max element in the matrix

    29

    4

  • Max Element

    2 3 1 2 1

    6 5 7 2 8

    2 1 1 5 3

    3 4 5 3 1

    0 1 2 3

    0

    1

    2

    3

    Exercise 7 :

    Write a nested for loop that will find the max element in the matrix

    30

    4

    maxElement = A[0][0]for i in range(len(A)):

    for j in range(len(A[0])):if (maxElement < A[i][j]):

    maxElement = A[i][j]

    print(maxElement)

  • More Exercises

    Given the following code to find the Max of a Matrix:

    31

    1. maxElement = A[0][0]2. for i in range(len(A)):3. for j in range(len(A[0])):4. if (maxElement < A[i][j]):5. maxElement = A[i][j]6. print(maxElement)

    Modify the code to find:

    Exercise 8: The Minimum in the Matrix

    Exercise 9: The Minimum of each row

    Exercise 10: The Minimum of each column

  • Min in Matrix

    32

    minElement = A[0][0]for i in range(len(A)):

    for j in range(len(A[0])):if (minElement > A[i][j]):

    minElement = A[i][j]print(minElement)

    Exercise 8: The Minimum in the Matrix

  • Min in each Row

    33

    for i in range(len(A)):minElement = A[i][0]for j in range(len(A[0])):

    if (minElement > A[i][j]):minElement = A[i][j]

    print(minElement)

    Exercise 9: Find the Minimum of each row

  • Min in each column

    34

    for i in range(len(A[0])):minElement = A[0][i]for j in range(len(A)):

    if (minElement > A[j][i]):minElement = A[j][i]

    print(minElement)

    Exercise 10: The Minimum of each column

  • Min in Matrix

    35