Python Forum
Matrix understanding in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix understanding in Python
#16
Don't give up quite yet. If you can understand this code, you are almost there:
import copy
matrix =   [[1, 0, 0, 0, 1], # matrix[0] index
            [0, 0, 0, 0, 0], # matrix[1] index
            [0, 1, 0, 0, 0], # matrix[2] index
            [0, 1, 0, 0, 0]] # matrix[3] index
matrix_copy = copy.deepcopy(matrix) # you need to make changes in a copy of the matrix based on the state of the first matrix
for row in range(len(matrix)) : # loop through the rows
    for col in range(len(matrix[0])) : #loop through the cols
        if matrix[row][col] == 1 : # if a cell is zombie, make neighbor cells zombies
            if row != 0 : # can't access row negative 1!
                matrix_copy[row-1][col] = 1
            if row != (len(matrix)-1) : # again checking for edge conditions
                matrix_copy[row+1][col] = 1
            if col != 0 :
                matrix_copy[row][col-1] = 1
            if col != (len(matrix[0])-1) :
                matrix_copy[row][col+1] = 1
                
print(matrix_copy) #This is your matrix after one time through
print(matrix) #original matrix unchanged
matrix = copy.deepcopy(matrix_copy) #copy so you can iterate
OK, a few things. The bunch of if statements check for border conditions. You want to change the cell above, below, left, and right but not left of a zero column cell or below the last cell on the matrix. If you just modify in the original matrix, then by modifying the cell to the right you will find another zombie cell and everything to the right gets changed. Same with down. So, must make modifications to a copy.

I used copy.deepcopy() because if I just use copy or "=" it copies the references and matrix and matrix_copy both end up pointing to the same cells. deepcopy() copies the objects in the lists.

To take this the rest of the way, you would need to write a quick loop through the matrix_copy that checks to see if there are any more zeros. Then (with proper indentation) put the whole thing except the initialization of the matrix into a while loop that exits when there are no more zeroes. The while loop needs to count the number of times it took to get to the all zombie state.

There are fancier ways to do this,tried to keep it simple.
Reply


Messages In This Thread
Matrix understanding in Python - by RavCOder - Nov-13-2019, 11:35 AM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 11:51 AM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 12:02 PM
RE: Matrix understanding in Python - by jefsummers - Nov-13-2019, 12:31 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 12:36 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 01:43 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 02:11 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 02:17 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 02:27 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 02:36 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 03:02 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 03:15 PM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 07:21 AM
RE: Matrix understanding in Python - by RavCOder - Nov-14-2019, 08:44 AM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 12:10 PM
RE: Matrix understanding in Python - by jefsummers - Nov-14-2019, 12:37 PM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 02:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,369 May-15-2019, 01:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020