How to print the entire row and column of a specific cell in a matrix with Python

2 Answers

0 votes
import numpy as np
 
matrix = np.array([ [1, 2, 3, 4, 5],
                    [0, 3, 6, 7, 8],
                    [9, 9, 6, 5, 4],
                    [8, 8, 8, 1, 1],
                    [2, 1, 1, 3, 5] ])
           
cell_row = 2
cell_col = 3
 
print(matrix[cell_row,: ])
print(matrix[:, cell_col])
 
 
 
'''
run:
 
[9 9 6 5 4]
[4 7 5 1 3]
 
'''

 



answered Jul 9, 2024 by avibootz
edited Jul 9, 2024 by avibootz
0 votes
def getColumn(matrix, i):
    return [row[i] for row in matrix]

matrix = [ [1, 2, 3, 4, 5],
           [0, 3, 6, 7, 8],
           [9, 9, 6, 5, 4],
           [8, 8, 8, 1, 1],
           [2, 1, 1, 3, 5] ]
          
cell_row = 2
cell_col = 3

print(matrix[cell_row])
print(getColumn(matrix, cell_col))



'''
run:

[9 9 6 5 4]
[4 7 5 1 3]

'''

 



answered Jul 9, 2024 by avibootz
...