Skip to content Skip to sidebar Skip to footer

Extract Array Indices That Contain A Specific Value

I have an array with the shape: (31777, 44, 44) Its sum is 31777.0, and every (44,44) grid contains just one 1.0 entry at some coordinates. My objective is to obtain an array of sh

Solution 1:

You can use the numpy.where to find the indexes of elements in multidimensional array, for example:

import numpy as np
arr = np.array([[[1,0],[0,0]], [[0,0],[1,0]], [[0,0], [0,1]]])
arr.shape
# (3, 2, 2)

np.array([coord for coord in zip(*np.where(arr == 1)[1:])])
# array([[0, 0],
#        [1, 0],
#        [1, 1]]) this is an array of shape (3,2) which contains coordinates of 1.0 entry.

Post a Comment for "Extract Array Indices That Contain A Specific Value"