How to change a single value in a NumPy array?

Question:

I want to change a single element of an array.
For example, I have:

A = np.array([1,2,3,4],
             [5,6,7,8],
             [9,10,11,12],
             [13,14,15,16])

I want to relace A[2][1] = 10 with A[2][1] = 150.
How can I do it?

Asked By: user8072140

||

Answers:

Is this what you are after? Just index the element and assign a new value.

A[2,1]=150

A
Out[345]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 150, 11, 12],
       [13, 14, 15, 16]])
Answered By: Allen Qin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.