2

does anyone know how to select multiple rows of a matrix to form a new one - e.g. I would like to select EVERY 3rd row of a matrix and build a new matrix with these rows.

Many thanks for your help,

Nicolas

Nicolas O
  • 143
  • 2
  • 12
  • It was really easy to find dozens of answers to existing questions asking pretty much the same thing ;) – zvone Sep 01 '18 at 15:23

1 Answers1

1

An example using numpys ndarray to create a matrix using 10 rows and 3 columns as an example

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
Stumpp
  • 279
  • 4
  • 7
  • 16