Il modo migliore nel tuo caso particolare sarebbe solo quello di cambiare i tuoi due criteri in un criterio:
dists[abs(dists - r - dr/2.) <= dr/2.]
Crea solo un array booleano, e secondo me è più facile da leggere perché dice, è distall'interno di un dro r? (Anche se vorrei ridefinire rdi essere il centro della tua regione di interesse anziché l'inizio, quindi r = r + dr/2.) Ma questo non risponde alla tua domanda.
La risposta alla tua domanda: in
realtà non ti serve wherese stai solo cercando di filtrare gli elementi distsche non corrispondono ai tuoi criteri:
dists[(dists >= r) & (dists <= r+dr)]
Perché &ti darà un elementally and(le parentesi sono necessarie).
Oppure, se si desidera utilizzare whereper qualche motivo, è possibile fare:
dists[(np.where((dists >= r) & (dists <= r + dr)))]
Perché:
il motivo per cui non funziona è perché np.whererestituisce un elenco di indici, non un array booleano. Stai cercando di passare andtra due elenchi di numeri, che ovviamente non hanno i valori True/ Falseche ti aspetti. Se ae bsono entrambi Truevalori, quindi a and brestituisce b. Quindi dire qualcosa del genere [0,1,2] and [2,3,4]ti darà [2,3,4]. Eccolo in azione:
In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1
In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)
In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),)
In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),)
Ciò che ti aspettavi di confrontare era semplicemente l'array booleano, per esempio
In [236]: dists >= r
Out[236]:
array([False, False, False, False, False, False, False, False, False,
False, True, True, True, True, True, True, True, True,
True, True], dtype=bool)
In [237]: dists <= r + dr
Out[237]:
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True, False, False, False, False, False,
False, False], dtype=bool)
In [238]: (dists >= r) & (dists <= r + dr)
Out[238]:
array([False, False, False, False, False, False, False, False, False,
False, True, True, True, False, False, False, False, False,
False, False], dtype=bool)
Ora puoi chiamare np.wherel'array booleano combinato:
In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)
In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. , 5.5, 6. ])
O semplicemente indicizza l'array originale con l'array booleano usando l' indicizzazione di fantasia
In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. , 5.5, 6. ])
()giro(ar>3)e(ar>6)?