Reputation: 45
I'm trying to come up with an algorithm for extracting a rectangular address and value from a 2D numpy array in python. The input looks something like this:
[[1, 1, 0, 0, 0, 0, 2],
[1, 1, 0, 0, 0, 0, 2],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 3, 3, 3, 0],
[0, 0, 0, 3, 3, 3, 0],
[0, 4, 4, 3, 3, 3, 0],
[0, 0, 0, 0, 0, 0, 3]]
Where there are various clusters of equal data at various locations in the array. I'm trying to output it to a format that consists of the cluster start addresses (startY, startX), the cluster end addresses (endY, endX), and the value of that cluster, in a rectangular format. So for the above 2D array above, the series of output values would look like this (I have excluded the zero values for the sake of brevity, but they would be included in the output):
(0, 0, 1, 1, 1)
(0, 6, 1, 6, 2)
(3, 3, 5, 5, 3)
(5, 1, 5, 2, 4)
(6, 6, 6, 6, 3)
Is there a function in numpy that can do something like this? I have looked through the documentation, and I can't seem to find anything, though admittedly I'm quite new to numpy, so I might be missing something, or not putting together the logic of nesting functions to make the process 2-dimensional.
Thank you!
Upvotes: 1
Views: 523
Reputation: 231540
I'm not seeing the relevance of the 2nd array.
Just eye balling the first array, I can index the non-zero blocks with
In [738]: arr[0:2,0:2]
Out[738]:
array([[1, 1],
[1, 1]])
In [739]: arr[0:2,6:7]
Out[739]:
array([[2],
[2]])
In [740]: arr[3:6,3:6]
Out[740]:
array([[3, 3, 3],
[3, 3, 3],
[3, 3, 3]])
In [741]: arr[5:6,1:3]
Out[741]: array([[4, 4]])
In [742]: arr[6:7,6:7]
Out[742]: array([[3]])
Upvotes: 1