I'm using more and more numpy
for fancy indexing possibilities, but this time I hit a hurdle, I can not solve the loop without being ugly.
There is a pair of my input vectors, a big vector v
and a small vector of the index e
. What I want to do is to get all the index i
equal to one of the v [i]
values v [e [0]], v [e] 1]], ... v [e] [n]]
. At the moment, this code is for me (and it works)
import nppy np v = np.array ([0,0,0,0,1,1, 1,2] , 2,2,2,2,2]) E = NP. Array ([0,4]) What I want to achieve is vector [0,1,2,3,4,5,6] In the values i = i [i] r = [] in the range (n): if v [i] values: r.append (i)
in case when E
is only one number, I am able to do this:
rr = np.arange (n) r = v [rr] == v [e] < / Code>
Which is better than the one for the loop, is there a way to do this when E
is not a single number?
You can use and:
& gt; & Gt; & Gt; V = np.array ([0,0,0,0,1,1,1,2,2,2,2,2,2]) & gt; & Gt; & Gt; E = [0,4]> gt; & Gt; & Gt; Np.in1d (v, v [e]) array ([true, true, true, true, true, true, true, wrong, wrong, wrong, wrong, wrong, wrong], dtype = Bool] gt; & Gt; & Gt; Np.where (np.in1d (v, v [e])) (array ([0, 1, 2, 3, 4, 5, 6]),) gt; & Gt; & Gt; Np.where (np.in1d (v, v [e])) [0] array ([0, 1, 2, 3, 4, 5, 6])
Comments
Post a Comment