3

I read the answer to "What is Julia equivalent of numpy's where function?", but do not yet see how the answer (ifelse) gives the user all the functionality of numpy.where. I have posted example code below:

    A = [0.0 0.9 0.0 0.99 0.0]

    a = 1:length(A)

    #-v- produces [0 1.0 0 1.0 0] as expected, but how to get the index values?
    b = ifelse.(A .- 1.0 .> -1.0, 1.0, 0 )
    #-^- how to get the array [0.9 0.99]? How to remove all zeros from an array?

Any workarounds other than using for loops would be appreciated.

elscan
  • 113
  • 1
  • 9

2 Answers2

9

I guess you're looking for the functionality of np.where(cond)? That's simply findall(A .> 0).

To get the array [0.9, 0.99], I'd use logical indexing: A[A .> 0].

mbauman
  • 30,958
  • 4
  • 88
  • 123
5

Potentially avoiding allocating the masking array would be faster, so filter(x -> x>0, A)

Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137