Rearranging text file order
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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'
0 个评论
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!