0

I want to copy over a specific array index from array A to array B if both arrays are equal

A = np.random.randint(0, 5, size=(5, 4))
B = a.copy()
B[0,0] = 10
B[:,3] = 999

B:

array([[ 10,   0,   4, 999],
       [  4,   3,   2, 999],
       [  1,   4,   3, 999],
       [  1,   3,   1, 999],
       [  3,   1,   1, 999]])

A:

array([[0, 0, 4, 3],
       [4, 3, 2, 2],
       [1, 4, 3, 2],
       [1, 3, 1, 4],
       [3, 1, 1, 3]])

now if A[:,0:3] == B[:,0:3] I want to replace B[:,3] with A[:,3]

like

array([[ 10,   0,   4, 999],
       [  4,   3,   2, 2],
       [  1,   4,   3, 2],
       [  1,   3,   1, 4],
       [  3,   1,   1, 3]])
1
  • np.where((A[:, 0:3] == B[:, 0:3]).all(1), A[:, 3], B[:, 3]). Just assign it back as needed Commented Aug 14, 2019 at 17:31

1 Answer 1

1

You can use np.copyto with the where keyword:

np.copyto(B[:,3],A[:,3],where=(A[:,:3]==B[:,:3]).all(1))
B
# array([[ 10,   0,   4, 999],
#        [  4,   3,   2,   2],
#        [  1,   4,   3,   2],
#        [  1,   3,   1,   4],
#        [  3,   1,   1,   3]])
Sign up to request clarification or add additional context in comments.

Comments

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.