Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ordering matrices by column

I Have this matrix:

matriz_senha:

[['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:

matriz senha:

[['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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading