我试图在数组A中查找所有索引,其中值大于time0且小于time1。在matlab中,我可以这样做:
[M,F] = mode( A((A>=time0) & (A<=time1)) ) %//only interested in range
我在IDL中有类似的东西,但是速度很慢:
tmpindex0 = where(A ge time0)
tmpindex1 = where(A lt time1)
M = setintersection(tmpindex0,tmpindex1)
其中setintersection()是函数,用于查找两个数组之间的相交元素。什么是快速的替代实现?
答案 0 :(得分:1)
您可以结合您的条件:
M = where(A ge time0 and A lt time1, count)
然后M
将包含指向time0
和time1
的索引,而count
将包含索引的数目。通常,您要在使用count
之前先检查M
。
答案 1 :(得分:0)
这有效(来自mgalloy答案的轻微修改):
M = where( (A ge time0) and (A lt time1), n_match, complement=F, n_complement=ncomp)
括号分隔不是必需的,但可以增加清晰度。 n_match
包含与您的条件匹配的数量,而补语F
将包含不匹配的索引,而ncomp
将包含不匹配的数量。