I have the following array
integer, dimension(4) :: my_array = (/160,0,230,0/)
I would like to find the indices of the elements that are non-zero and store them in another array or individual variables for future use. I am not sure how to do this because I don't know a prior how many elements are non-zero. I was thinking of using a loop combined with count(my_array/=0) and maxloc.
Is some sort of loop the only way? I can't think of a good way to use WHERE or FINDLOC.
I have tried this
ii = COUNT(my_array.NE.0)
ALLOCATE(choices(ii))
choices = PACK(my_array,my_array.NE.0)
But only makes a new array without zero elements, so I lose the original indices.