I have a 2D array, and I hope I can get all of the non-zero data and their position. But now I just can get the non-zero data position.
Is there any way that I can get the value and the position at the same time?
import numpy as np
groupMatrix = np.array([
[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 2],
[3, 3, 0, 2]
])
res = zip(*np.nonzero(groupMatrix))
print(list(res))
The result I got is [(0, 0), (0, 1), (1, 0), (2, 3), (3, 0), (3, 1), (3, 3)]
But I hope the result can show the position with value.
For example, (x, y, z): x, y represent the position and z represents the value at the point.
(0, 0, 1), (0, 1, 1), (1, 0, 1), (2, 3, 2), (3, 0, 3), (3, 1, 3), (3, 3, 2).
Thank you for your help!!!