2017-03-17 101 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

感謝您的回答。 –