Rearranging text file order

2 Ansichten (letzte 30 Tage)
Nicholas Ng
Nicholas Ng am 17 Okt. 2014
Bearbeitet: Guillaume am 17 Okt. 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

Antworten (2)

Michael Haderlein
Michael Haderlein am 17 Okt. 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 am 17 Okt. 2014
Bearbeitet: Guillaume am 17 Okt. 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]

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by