Home > Back-end >  Ordering matrices by column
Ordering matrices by column

Time:11-17

I Have this matrix:

matrix:

[['I' 'N' 'T' 'E' 'R' 'E' 'S' 'T' 'I' 'N' 'G']

['D' 'G' 'F' 'F' 'G' 'D' 'A' 'A' 'D' 'V' 'A']

['A' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']]

Want to order like this, without changing the position of the following rows in alphabethic order, only the first row:

matrix

[['E' 'E' 'G' 'I' 'I' 'N' 'N' 'R' 'S' 'T' 'T']

 ['F' 'D' 'A' 'D' 'D' 'G' 'V' 'G' 'A' 'F' 'A']

 [' ' ' ' ' ' 'A' ' ' ' ' ' ' ' ' ' ' ' ' ' ']]

Is there any way i can do this with numpy?

CodePudding user response:

Assuming this input:

array([['I', 'N', 'T', 'E', 'R', 'E', 'S', 'T', 'I', 'N', 'G'],
       ['D', 'G', 'F', 'F', 'G', 'D', 'A', 'A', 'D', 'V', 'A'],
       ['A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']],
      dtype='<U1')

Use indexing and np.argsort:

out = matriz_senha[:, np.argsort(matriz_senha[0])]

Output:

array([['E', 'E', 'G', 'I', 'I', 'N', 'N', 'R', 'S', 'T', 'T'],
       ['F', 'D', 'A', 'D', 'D', 'G', 'V', 'G', 'A', 'F', 'A'],
       [' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ']],
      dtype='<U1')
  • Related