【问题标题】:Python: Selecting every Nth row of a matrix [duplicate]Python:选择矩阵的每第 N 行 [重复]
【发布时间】:2018-09-01 15:18:26
【问题描述】:

有谁知道如何选择矩阵的多行来形成一个新行 - 例如我想选择矩阵的每 3 行并用这些行构建一个新矩阵。

非常感谢您的帮助,

尼古拉斯

【问题讨论】:

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

标签: python matrix


【解决方案1】:

以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2013-07-29
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2022-12-12
    相关资源
    最近更新 更多