简体   繁体   中英

How to replace a value in a matrix by index

Let's say I have a 4X4 matrix containing value from 1 to 20. If I want the element at line 2 column 3 to be equal to 100, how can I do this?

First, import the numpy library:

import numpy as np

Then, create the matrix:

matrix = np.array([[1,2,3], [4,5,6], [7,8,9]])

Finally, use the index to replace the value:

matrix[1,2] = 10

The new matrix will be:

[[ 1  2  3]
 [ 4  5 10]
 [ 7  8  9]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM