Rearranging text file order

1 visualización (últimos 30 días)
Nicholas Ng
Nicholas Ng el 17 de Oct. de 2014
Editada: Guillaume el 17 de Oct. de 2014
Hi guys. I have a text file that has a few rows and columns: Name WIN LOSE
I have a few data of names, win, and lose sorted.
I'm having difficulties figuring out a way to rearrange the text file data from higher to lower win score while bringing the particular lose and name row specifics along .. how do i do this ? Please help me out :S

Respuestas (2)

Michael Haderlein
Michael Haderlein el 17 de Oct. de 2014
I'm not 100% sure if I understood your question correctly, but this might be what you need:
>> name={'A';'B';'C'};
>> score=[3;1;2];
>> [sort_score,ind]=sort(score);
>> sort_name=name(ind);
>> sort_score
sort_score =
1
2
3
>> sort_name
sort_name =
'B'
'C'
'A'

Guillaume
Guillaume el 17 de Oct. de 2014
Editada: Guillaume el 17 de Oct. de 2014
You're not telling us how you're storing the content on your text file, so I'm assuming a cell array. In any case, to sort data according to a column and bring along the others, use sortrows:
scores = {'Player 1', 5, 3
'Player 2', 4, 9
'Player 3', 7, 1};
sortrows(scores, 2)
>> ans =
'Player 2' [4] [9]
'Player 1' [5] [3]
'Player 3' [7] [1]

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by