Python:选择矩阵的每N行
是否有人知道如何选择矩阵的多行以形成一个新的矩阵?例如,我想选择矩阵的每三行并用这些行构建一个新矩阵 非常感谢你的帮助Python:选择矩阵的每N行,python,matrix,Python,Matrix,是否有人知道如何选择矩阵的多行以形成一个新的矩阵?例如,我想选择矩阵的每三行并用这些行构建一个新矩阵 非常感谢你的帮助 Nicolas以使用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
Nicolas以使用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
对于现有的问题,很容易找到几十个答案,问的几乎都是相同的问题;)