【问题标题】:Can't access the elements of a Matrix无法访问矩阵的元素
【发布时间】:2017-03-17 23:25:55
【问题描述】:

我是 python 新手。如果这个问题看起来很新手,请不要对我苛刻。我创建了一个这样的矩阵:

Matrix = np.matrix([[0,0,0,1], [0,0,1,1], [1,0,0,1], [1,1,0,0],[0,0,1,1]])

现在,当我尝试访问 Matrix 的元素时,如下所示:

import numpy as np    
print(Matrix[0][3])

我收到以下错误:

IndexError:索引 1 超出轴 0 的范围,大小为 1

我已经浏览过 stackoverflow 上的所有相关帖子,但到目前为止还没有找到解决方案。

【问题讨论】:

标签: python matrix


【解决方案1】:

我认为你想要的语法是:

>>> import numpy as np
>>> Matrix = np.matrix([[0,0,0,1], [0,0,1,1], [1,0,0,1], [1,1,0,0],[0,0,1,1]])
>>> print(Matrix[0,3])
1

【讨论】:

    【解决方案2】:

    您需要编写逗号分隔的索引

    print(Matrix[0, 3])
    

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2020-09-06
    • 2020-02-18
    相关资源
    最近更新 更多