0

I have an array

a = np.ones((5, 5))

that looks like this

[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]

and then another array

b = np.array([0, 0, 0, 1, 0])

and I am struggling to find a piece of code in Numpy documentation that would add array b to the specific row in array a

for example I would like to add array b only to the 3rd row of array a so that

c = [1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1]
    [1, 1, 1, 2, 1]
    [1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1]
1
  • a[3,:] += b might work for you. Commented Mar 8, 2019 at 17:51

1 Answer 1

2

Use this:

a[2] += b

Output:

print (a)
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 2. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

If you don't want to modify a, and want the result in a different array c, it's best done as a two step process:

c = np.copy(a)
c[2] += b
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.