0

Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?

res = np.array([], dtype=np.float64).reshape(0,6)
for line in edata:
    ind = line[1] == tdata[:,1]  
    ind = line[2] == tdata[ind,2]
    if np.any(ind):
        res = np.vstack((res,line))

1 Answer 1

1

You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:

res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
              &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]

for example, with simple input:

edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12

print (res)
array([[12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata

Sign up to request clarification or add additional context in comments.

4 Comments

check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
Sry, i was checking with 1st and 2nd column, my bad
@Eular no pb :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.