简体   繁体   中英

How do I compare non-zero values over multiple 2D numpy arrays?

Let's say I have two 2D arrays filled with random integers. I want the first array to retain all indices with values leq 5, and the second array to retain all indices with values leq 6. For simplicity, all other indices can be set to zero.

table1 = np.array(([1,3],[5,7]))
table2  = np.array(([2,4],[6,8]))

print table1
print table2 ,'\n'

table1_tol_i = np.where(table1[:,:] > 5)
table1[table1_tol_i] = 0
print table1 

table2_tol_i = np.where(table2[:,:] > 4)
table2[table2_tol_i] = 0
print table2, '\n'

[[1 3]
 [5 7]]
[[2 4]
 [6 8]] 

[[1 3]
 [5 0]]
[[2 4]
 [0 0]]

I then want to retain only the indices where both tables have non-zero values. I want

[[1 3]
 [0 0]]
[[2 4]
 [0 0]]

I tried the following but it doesn't give me what I want

nonzero_i = (np.where(table1 != 0) and (np.where(table2 != 0 )))
print table1[nonzero_i]
print table2[nonzero_i]

[1 3]
[2 4]

You are retaining only the indices where both tables have non-zero values - that's why when you index into table1 and table2 with nonzero_i , you return only the first row (which contains only non-zero values).

This logic could be expressed more simply by constructing a boolean index array :

# boolean array that is True wherever both table1 & table2 are nonzero
valid = (table1 != 0) & (table2 != 0)

# or alternatively:
# valid = np.logical_and(table1, table2)

You could use this to index directly into table1 and table2 .


It seems that what you actually want to do is to assign a value of 0 to any location where either table1 or table2 is 0.

If you want a copy, you could use the np.where(condition, val_where_true, val_where_false) syntax of np.where :

print(repr(np.where(valid, table1, 0)))
# array([[1, 3],
#        [0, 0]])

print(repr(np.where(valid, table2, 0)))
# array([[2, 4],
#        [0, 0]])

Or if you wanted to modify table1 and table2 in place, you could do

table1[~valid] = 0

table2[~valid] = 0

( ~ is the "bitwise not" operator, which inverts the boolean index array).

Get all indices where table 1 is 0:

table1_zeros = np.where(table1[:,:] == 0)

Get all indices where table 2 is 0:

table2_zeros = np.where(table2[:,:] == 0)

Set values on both tables to 0:

table1[table1_zeros] = 0
table1[table2_zeros] = 0
table2[table1_zeros] = 0
table2[table2_zeros] = 0

You could also do this in one step by using tableX_tol_i in place of tableX_zeros .

table1 = np.array(([1,3],[5,7]))
table2  = np.array(([2,4],[6,8]))
table3  = np.zeros((2,2))
for k in range(0,2):
    for j in range(0,2):
        if table1[k][j] > 5:
            table1[k][j] = 0
        if table1[k][j] <= 5:
            table1[k][j] = table1[k][j]
        if table2[k][j] > 6:
            table2[k][j] = 0
        if table2[k][j] <= 6:
            table2[k][j] = table2[k][j]
x=0
y=0
for k in range(0,2):
    for j in range(0,2):
        if table1[k][j] != 0:
            table3[x,y] = table1[k][j] 
            x = x+1
x=0
y=1
for k in range(0,2):
    for j in range(0,2):
        if table2[k][j] != 0:
            table3[x,y] = table2[k][j] 
            x = x+1   
print (table3)   

I think it is the simplest way, what you want. You can modify the code, if your array size changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM