0

I have two numpy arrays, lets say

A = array([ [a11, a12, a13], [a21, a22, a23], [a31, a32, a33] ])
B = array([ [b11, b12, b13], [b21, b22, b23], [b31, b32, b33] ])

and I want to get (EDITED)

C = array([ [a11, a12+b12, a13], [a21, a22+b22, a23], [a31, a32+b32, a33] ])

I could achieve this with for-loop, but I believe there must be a more elegant way in Python.

0

1 Answer 1

2

Can you not simply do the following:

C = A.copy()
C[1] += B[1]    # (or appropriate indexes)

OK -- edited solution:

C = A.copy()
C[:,1] += B[:,1]
Sign up to request clarification or add additional context in comments.

2 Comments

sorry I wrote the C array wrong. It's supposed to be C = array([ [a11, a12+b12, a13], [a21, a22+b22, a23], [a31, a32+b32, a33] ])
but I see your answer is correct anyway - I can just use C = A.copy() C[:,1] += B[:,1] works fine

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.