简体   繁体   English

Python中2D矩阵的元素

[英]Element of 2D matrix in Python

I have the following Python code to display the matrix element, but it does not show what is expected as I want to show the first element instead of first line elements. 我有以下Python代码来显示matrix元素,但是由于我想显示第一个元素而不是第一行元素,所以它没有显示预期的结果。 What is the problem? 问题是什么?

import numpy as np

ss = [-0.115, -0.052, 0.559, -0.344, 0.077, 0.032, -0.017, -0.035]
sx = np.matrix([[ss[0],ss[1],ss[2],ss[3]],[ss[4],ss[5],ss[6],ss[7]]])

print('s0=', sx[0])
print('s00=', sx[0][0])

For accessing a first row in an 2d array 用于访问2d数组中的第一行

print('s0=', sx[0,:]))  # replace zero with row you want to access

And to access first element. 并访问第一个元素。

print('s00=', sx.item(0,0)) # replace 0,0 with the position of element 

For documentation refer 有关文档, 请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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