Q:

Write a NumPy program to find and store non-zero unique rows in an array after comparing each row with other row in a given matrix

0

Write a NumPy program to find and store non-zero unique rows in an array after comparing each row with other row in a given matrix.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
arra = np.array([[ 1,  1,  0],
                 [ 0,  0,  0],
                 [ 0,  2,  3],
                 [ 0,  0,  0],
                 [ 0, -1,  1],
                 [ 0,  0,  0]])

print("Original array:")
print(arra)
temp = {(0, 0, 0)}
result = []
for idx, row in enumerate(map(tuple, arra)):
    if row not in temp:
        result.append(idx)
print("\nNon-zero unique rows:")
print(arra[result])

Sample Output:

Original array:
[[ 1  1  0]
 [ 0  0  0]
 [ 0  2  3]
 [ 0  0  0]
 [ 0 -1  1]
 [ 0  0  0]]

Non-zero unique rows:
[[ 1  1  0]
 [ 0  2  3]
 [ 0 -1  1]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now