how to Sort on the basis of a particular column
3 views (last 30 days)
Show older comments
' are going where you ' '2.050835e-011'
' are going you where ' '3.968437e-012'
' are where going you ' '1.224379e-012'
' are where you going ' '1.442999e-010'
' are you going where ' '1.802050e-009'
' are you where going ' '4.201020e-010'
' going are where you ' '7.568890e-013'
' going are you where ' '2.179447e-011'
' going where are you ' '1.217540e-008'
' going where you are ' '3.561656e-013'
' going you are where ' '3.561656e-013'
' going you where are ' '1.583239e-011'
' where are going you ' '7.839951e-008'
' where are you going ' '8.208695e-005'
' where going are you ' '6.443150e-009'
' where going you are ' '2.037477e-011'
' where you are going ' '4.066607e-009'
' where you going are ' '4.282509e-010'
' you are going where ' '9.933958e-012'
' you are where going ' '7.066954e-012'
' you going are where ' '1.246560e-011'
' you going where are ' '1.347531e-009'
' you where are going ' '5.261888e-009'
' you where going are ' '2.438801e-013'
' are going where you ' '2.050835e-011'
' are going you where ' '3.968437e-012'
' are where going you ' '1.224379e-012'
' are where you going ' '1.442999e-010'
' are you going where ' '1.802050e-009'
' are you where going ' '4.201020e-010'
' going are where you ' '7.568890e-013'
' going are you where ' '2.179447e-011'
' going where are you ' '1.217540e-008'
' going where you are ' '3.561656e-013'
' going you are where ' '3.561656e-013'
' going you where are ' '1.583239e-011'
' where are going you ' '7.839951e-008'
' where are you going ' '8.208695e-005'
' where going are you ' '6.443150e-009'
' where going you are ' '2.037477e-011'
' where you are going ' '4.066607e-009'
' where you going are ' '4.282509e-010'
' you are going where ' '9.933958e-012'
' you are where going ' '7.066954e-012'
' you going are where ' '1.246560e-011'
' you going where are ' '1.347531e-009'
' you where are going ' '5.261888e-009'
' you where going are ' '2.438801e-013'
I want to sort to these cell array on the basis of second column and i am using following code
* |sorted_matrix = sortrows(sentences,2);| *
and the output is coming unexpected as given below
' going where are you ' '1.217540e-008'
' are where going you ' '1.224379e-012'
' you going are where ' '1.246560e-011'
' you going where are ' '1.347531e-009'
' are where you going ' '1.442999e-010'
' going you where are ' '1.583239e-011'
' are you going where ' '1.802050e-009'
' where going you are ' '2.037477e-011'
' are going where you ' '2.050835e-011'
' going are you where ' '2.179447e-011'
' you where going are ' '2.438801e-013'
' going where you are ' '3.561656e-013'
' going you are where ' '3.561656e-013'
' are going you where ' '3.968437e-012'
' where you are going ' '4.066607e-009'
' are you where going ' '4.201020e-010'
' where you going are ' '4.282509e-010'
' you where are going ' '5.261888e-009'
' where going are you ' '6.443150e-009'
' you are where going ' '7.066954e-012'
' going are where you ' '7.568890e-013'
' where are going you ' '7.839951e-008'
' where are you going ' '8.208695e-005'
' you are going where ' '9.933958e-012'
,what i have to write to get expected result.
sorted on the basis of second column in ascending order.
0 Comments
Accepted Answer
Image Analyst
on 6 Oct 2013
Try this:
ca = {...
' are going where you ' '2.050835e-011'
' are going you where ' '3.968437e-012'
' are where going you ' '1.224379e-012'
' are where you going ' '1.442999e-010'
' are you going where ' '1.802050e-009'
' are you where going ' '4.201020e-010'
' going are where you ' '7.568890e-013'
' going are you where ' '2.179447e-011'
' going where are you ' '1.217540e-008'
' going where you are ' '3.561656e-013'
' going you are where ' '3.561656e-013'
' going you where are ' '1.583239e-011'
' where are going you ' '7.839951e-008'
' where are you going ' '8.208695e-005'
' where going are you ' '6.443150e-009'
' where going you are ' '2.037477e-011'
' where you are going ' '4.066607e-009'
' where you going are ' '4.282509e-010'
' you are going where ' '9.933958e-012'
' you are where going ' '7.066954e-012'
' you going are where ' '1.246560e-011'
' you going where are ' '1.347531e-009'
' you where are going ' '5.261888e-009'
' you where going are ' '2.438801e-013'
' are going where you ' '2.050835e-011'
' are going you where ' '3.968437e-012'
' are where going you ' '1.224379e-012'
' are where you going ' '1.442999e-010'
' are you going where ' '1.802050e-009'
' are you where going ' '4.201020e-010'
' going are where you ' '7.568890e-013'
' going are you where ' '2.179447e-011'
' going where are you ' '1.217540e-008'
' going where you are ' '3.561656e-013'
' going you are where ' '3.561656e-013'
' going you where are ' '1.583239e-011'
' where are going you ' '7.839951e-008'
' where are you going ' '8.208695e-005'
' where going are you ' '6.443150e-009'
' where going you are ' '2.037477e-011'
' where you are going ' '4.066607e-009'
' where you going are ' '4.282509e-010'
' you are going where ' '9.933958e-012'
' you are where going ' '7.066954e-012'
' you going are where ' '1.246560e-011'
' you going where are ' '1.347531e-009'
' you where are going ' '5.261888e-009'
' you where going are ' '2.438801e-013'}
% Sort based on the numerical value of column 2.
[sortedCol2, sortOrder] = sort(str2double(ca(:,2)), 'ascend')
% Create a new cell array based on that sorting order.
ca2 = ca(sortOrder, :)
0 Comments
More Answers (1)
dpb
on 6 Oct 2013
Your data are cell strings not numeric values, hence the resulting sort is on the collating order of the characters, not the numeric value. So, the result is "expected"... :)
Sort on the numeric value of the second column, not the text -- since you don't want collating order, sortrows isn't of direct help here, instead return the index and then rearrange --
[~,ix]=sort(str2num(char(s(:,2))));
sorted=s(ix,:);
where I've used s as the character array for brevity
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!