Rearranging text file order

3 次查看(过去 30 天)
Nicholas Ng
Nicholas Ng 2014-10-17
编辑: Guillaume 2014-10-17
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

回答(2 个)

Michael Haderlein
Michael Haderlein 2014-10-17
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 2014-10-17
编辑:Guillaume 2014-10-17
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]

标签

Community Treasure Hunt

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

Start Hunting!

Translated by