Permuter deux lignes d'une matrice avec numpy sous python

Published: 21 janvier 2015

DMCA.com Protection Status

Exemple de comment permuter deux lignes d'une matrice avec numpy sous python (on cherche ici à permuter les lignes d'indice 1 et 2 de la matrice M):

>>> import numpy as np
 >>> M = np.arange(20).reshape(4,5)
 >>> M
 array([[ 0,  1,  2,  3,  4],
   [ 5,  6,  7,  8,  9],
   [10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19]])
 >>> T = M[1,:].copy()
 >>> M[1,:] = M[2,:]
 >>> M[2,:] = T
 >>> M
 array([[ 0,  1,  2,  3,  4],
   [10, 11, 12, 13, 14],
   [ 5,  6,  7,  8,  9],
   [15, 16, 17, 18, 19]])

Recherches associées

Liens Site
numpy.copy Numpy doc