开发者

Nonzero function help, Python Numpy

开发者 https://www.devze.com 2023-03-23 16:36 出处:网络
I have two arrays, and I have a complex condition like this: new_arr<0 and old_arr>0 I am using nonzero but I am getting an error. The code I have is this:

I have two arrays, and I have a complex condition like this: new_arr<0 and old_arr>0 I am using nonzero but I am getting an error. The code I have is this:

    indices = nonzero(new_arr<0 and old_arr>0)

I tried:

    indices = nonzero(new_arr<0) and nonzero(old_arr>0)

But it gave me incorrect results.

Is there any way around this? And is there a way to get the common indices from two nonzero statements. For example, if:

    indices1 = nonzero(new_arr<0)
    indices2 = nonzero(old_arr>0)

and these two indices would contain:

   indices1 = array([0, 1, 3])
   indices2 = array([2, 3, 4])

The correct result would be getting the common element from these two (in this case it would be the element 3). Something like this:

    result = common(indices1, ind开发者_高级运维ices2)


Try indices = nonzero((new_arr < 0) & (old_arr > 0)):

In [5]: import numpy as np

In [6]: old_arr = np.array([ 0,-1, 0,-1, 1, 1, 0, 1])

In [7]: new_arr = np.array([ 1, 1,-1,-1,-1,-1, 1, 1])

In [8]: np.nonzero((new_arr < 0) & (old_arr > 0))
Out[8]: (array([4, 5]),)


Try

indices = nonzero(logical_and(new < 0, old > 0))

(Thinking about it, my previous example wasn't all that useful if all it did was return nonzero(condition) anyway.)

0

精彩评论

暂无评论...
验证码 换一张
取 消