python矩阵索引
时间: 2023-12-02 09:03:04 浏览: 68
Python矩阵常见运算操作实例总结
5星 · 资源好评率100%
以下是Python中矩阵索引的例子:
1. 取矩阵的所有行中所有列的值
```python
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[:,:]
print(b)
# 输出:
# [[1 2 3]
# [3 4 5]
# [4 5 6]]
```
2. 取矩阵的第一行
```python
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[0,:]
print(b)
# 输出:[1 2 3]
```
3. 取矩阵的第一列
```python
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[:,0]
print(b)
# 输出:[1 3 4]
```
4. 取矩阵的第一行和第二行
```python
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[0:2,:]
print(b)
# 输出:
# [[1 2 3]
# [3 4 5]]
```
5. 将每一行的索引为0和索引为2的数调换位置
```python
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[:,[1,0,2]]
print(b)
# 输出:
# [[2 1 3]
# [4 3 5]
# [5 4 6]]
```
阅读全文