Take indices of non zero elements of matrix

Joined
Jun 15, 2022
Messages
4
Reaction score
0
Hi,
Code:
1  0  -1
0  1   4
3  0   0

matrix build by this code:

Code:
n = int(input())  # n=3

for i in range(n):
        a =[]
        for j in range(n):
             a.append(int(input()))
        adjacency_matrix.append(a)

I want to save (0,0) (0,2) (1,1) (1,2) (2,0), position of non zero elements, for example like this:

Code:
x =[
0 0
0 2
1 1
1 2
2 0]
 
Joined
May 11, 2022
Messages
60
Reaction score
6
so thats fairly straight forward.
Python:
n = int(input())
coordinate_matrix =[]
for i in range(n):
    a = []
    for j in range(n):
        a.append(int(input()))
        if a[-1] != 0:
            coordinate_matrix +=[[i,j]]
     adjacency_matrix.append(a)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top