Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:选择矩阵的每N行_Python_Matrix - Fatal编程技术网

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

对于现有的问题,很容易找到几十个答案,问的几乎都是相同的问题;)