【发布时间】:2018-09-01 15:18:26
【问题描述】:
有谁知道如何选择矩阵的多行来形成一个新行 - 例如我想选择矩阵的每 3 行并用这些行构建一个新矩阵。
非常感谢您的帮助,
尼古拉斯
【问题讨论】:
-
很容易找到几十个现有问题的答案,问的问题几乎相同;)
有谁知道如何选择矩阵的多行来形成一个新行 - 例如我想选择矩阵的每 3 行并用这些行构建一个新矩阵。
非常感谢您的帮助,
尼古拉斯
【问题讨论】:
以使用numpys ndarray创建10行3列矩阵为例
import numpy as np
matrix = np.ndarray(shape=(10,3))
rows = np.shape(matrix)[0] #number of rows
columns = np.shape(matrix)[1] #number of columns
l = range(rows)[0::3] #indexes of each third element including the first element
new_matrix = np.ndarray(shape=(len(l),columns)) #Your new matrix
for i in range(len(l)):
new_matrix[i] = matrix[l[i]] #adding each third row from matrix to new_matrix
【讨论】: