0

I am trying to find all indices in an array A, where the value larger than time0 and less than time1. In matlab I can do:

[M,F] = mode(  A((A>=time0) & (A<=time1))  ) %//only interested in range

I have something similar in IDL but really slow:

tmpindex0 = where(A ge time0)   
tmpindex1 = where(A lt time1)   
M = setintersection(tmpindex0,tmpindex1)  

where setintersection() is function find the intersected elements between two arrays. What is the fast alternative implementation?

mgalloy
  • 2,356
  • 1
  • 12
  • 10
colddie
  • 1,029
  • 1
  • 15
  • 28

2 Answers2

1

You can combine your conditions:

M = where(A ge time0 and A lt time1, count)

Then M will contain indices into time0 and time1 while count will contain the number of indices. Generally, you want to check count before using M.

mgalloy
  • 2,356
  • 1
  • 12
  • 10
0

This works (slight modification from mgalloy answer):

M = where( (A ge time0) and (A lt time1), n_match, complement=F, n_complement=ncomp)

The parenthetical separation is not necessary but adds clarity. n_match contains the number of matches to your conditions whereas the complement F will contain the indices for the non-matches and ncomp will contain the number of non-matches.

cypher
  • 13
  • 1
  • 3