【问题标题】:How to replace a value in a matrix by index如何通过索引替换矩阵中的值
【发布时间】:2022-12-04 08:31:57
【问题描述】:

假设我有一个包含 1 到 20 值的 4X4 矩阵。如果我希望第 2 行第 3 列的元素等于 100,我该怎么做?

【问题讨论】:

    标签: python


    【解决方案1】:
    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:
    

    矩阵[1,2] = 10

    The new matrix will be:
    
    [[ 1  2  3]
     [ 4  5 10]
     [ 7  8  9]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 2012-08-19
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2019-07-04
      相关资源
      最近更新 更多