Skip to main content
  1. About
  2. For Teams
Asked
Viewed 2k times
3

so maybe this is a basic question about numpy, but I can't see how to do is, so lets say I have a 2D numpy array like this

import numpy as np

arr = np.array([[  0., 460., 166., 167., 123.],
                [  0.,   0.,   0.,   0.,   0.],
                [  0.,  81.,   0.,  21.,   0.],
                [  0., 128.,  23.,   0.,  12.],
                [  0.,  36.,   0.,  13.,   0.]])

And I want the coordinates from the subarray

[[0., 21,.  0.],
 [23., 0., 12.],
 [0., 13.,  0.]]

I tried slicing my original array and the find the coordinates using np.argwhere like this

newarr = np.argwhere(arr[2:, 2:] != 0)

#output
#[[0 1]
# [1 0]
# [1 2]
# [2 1]]

Which are indeed the coordinates from the subarray but I was expecting the coordinates corresponding to my original array, the desired output is:

[[2 3]
 [3 2]
 [3 4]
 [4 3]]

If I use the np.argwhere with my original array I get a bunch of coordinates that I don't need, so I can't figure it out how to get what I need, any help or if you can point me to the right direction will be great, thank you!

0

2 Answers 2

3

Assume origin on the top left corner of the matrix and the matrix itself placed in 4th quadrant of Cartesian space. The horizontal axis having the column indices, and the vertical axis coming down having row indices.

You will see the whole sub-matrix is origin shifted on (2,2) coordinate. Thus when the coordinates you get are with respect to sub-matrix on origin, then to get them back to (2,2) again, just add (2,2) in whole elements:

>>> np.argwhere(arr[2:, 2:] != 0) + [2, 2]
array([[2, 3],
       [3, 2],
       [3, 4],
       [4, 3]])

For other examples:

>>> col_shift, row_shift = 3, 2

>>> arr[row_shift:, col_shift:]
array([[21.,  0.],
       [ 0., 12.],
       [13.,  0.]])

>>> np.argwhere(arr[row_shift:, col_shift:] != 0) + [row_shift, col_shift]
array([[2, 3],
       [3, 4],
       [4, 3]])

For a fully inside sub matrix, you can bound the column and rows:

>>> col_shift, row_shift = 0, 1
>>> col_bound, row_bound = 4, 4

>>> arr[row_shift:row_bound, col_shift:col_bound]
array([[  0.,   0.,   0.,   0.],
       [  0.,  81.,   0.,  21.],
       [  0., 128.,  23.,   0.]])

>>> np.argwhere(arr[row_shift:row_bound, col_shift:col_bound] != 0) + [row_shift, col_shift]
array([[2, 1],
       [2, 3],
       [3, 1],
       [3, 2]])
Sign up to request clarification or add additional context in comments.

1 Comment

Carlos Eduardo Corpus
Oh I see what you meant! Thank you!
0

You have moved down the array two times and two times to the right. All that remains for you is to add the number of steps taken towards X and towards Y in the coordinates:

y = 2
x = 2

newarr = np.argwhere(arr[y:, x:] != 0)
X = (newarr[0:, 0] + x).reshape(4,1)
Y = (newarr[0:, 1] + y).reshape(4,1)
print(np.concatenate((X, Y), axis=1))

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.