Учитывается следующая матрица 3x3:
Код: Выделить всё
[[1 1 3]
[4 5 2]
[3 0 0]]
Код: Выделить всё
[2,2,1,2,1,1]
Код: Выделить всё
import numpy as np
m = np.matrix([[1,1,3], [4,5,2], [3,0,0]]) # Primary matrix, used for obtaining the values
print("Original Matrix: ") # Print original matrix
print(m)
print("Occurrences: ")
for i in range(6):
occur = (np.count_nonzero(m == i)) # Count the number of elements satisfying the condition
print(i, ":", occur)
Код: Выделить всё
Occurrences:
0 : 2
1 : 2
2 : 1
3 : 2
4 : 1
5 : 1
Подробнее здесь: https://stackoverflow.com/questions/764 ... sing-numpy