2017-03-17 117 views
-1

我是python的新手。如果这个问题看起来新手,请不要对我苛刻。我创造了这样一个矩阵:无法访问矩阵的元素

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

现在,当我尝试访问矩阵的元素,像这样:

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

我收到以下错误:

IndexError: index 1 is out of bounds for axis 0 with size 1

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

+0

为什么搜索的SO,而不是坚持到[官方文档(https://开头docs.scipy.org/doc/numpy-dev/user/quickstart.html)? – sascha

+0

[在numpy/scipy中切片数组]可能重复(http://stackoverflow.com/questions/2725750/slicing-arrays-in-numpy-scipy) –

+0

@MuhammadUsman我正在尝试打印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 
+0

它的工作。感谢你的回答。 –

0

你需要写逗号分隔指数

print(Matrix[0, 3]) 
+0

感谢您的回答。 –