I have a NxM matrix encoded in 0s and 1s - where only the 1s are to be printed in its respective locations whereas the 0s are blank spaces, such as the following matrix:
m = [[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1]
My question is how can I create a starting point from the change that takes place from a 0 to a 1 and an end point from the change that takes place from a 1 to a 0 in each row. Then print all the 1s between the start point and end point
I have the following code (which does not work):
nrows, ncols = m.shape #gets the shape of the matrix
for r in range(nrows):
for c in range(ncols):
if m[r,c] == 0 and m[r,c+1] == 1: #checks if there is a 0 first and then a 1 in the next index of the column in the row to create a starting point
start = m[r,c+1]
if m[r,c] == 1 and m[r,c+1] == 0: #checks if there is a 1 first and then a 0 in the next index of the column in the row to create an end point
end = m[r, c+1]
My desired output is, for example taking the lastrow into consideration:
It should print everything between the first 1 and last 1 before a 0 is found in that row, excluding the 0s. So basically this: 1 1 1 1....1 1 1 1 1 1....1 1 The dots (.)represent the 0s that have to be excluded All help and advice will be highly appreciated.