Suppose I have an array that contains zero and non-zero values. Now I want to find the index of non-zero values of my array. I tried many methods previously, but they are not giving me the desired result. Can someone help me with this?
NumPy offers an efficient function that you can use for this problem of finding the index of your desired values. You can simply specify a condition that identifies your desired value and this function returns the indices for the True
results of that condition. Here is an example of this function which finds the non-zeros elements in an array:
The resulting indices
variable contains a tuple of arrays, with the first (and only) array containing the indices of the non-zero elements.
Hey @safa , you can use the argwhere()
function in NumPy is a variation of the where()
function that returns the indices of all elements in an array that satisfy a given condition, not just the non-zero elements.