MATLAB Answers

0

Sorting a table in Matlab

Asked by Minjun Seo on 23 Aug 2019
Latest activity Answered by David K.
on 23 Aug 2019
I have a table of values that are either string or doubles. I want to sort them alphabetically for strings or least to greatest for numbers. Is there a way that involves not converting the table to a matrix? If there isn't how would I go by converting the table into a matrix and to a table again?

  0 Comments

Sign in to comment.

1 Answer

David K.
Answer by David K.
on 23 Aug 2019
 Accepted Answer

Yes, you can do it with the sortrows function. Here is an example:
A = [1 2 4 5 3 6 5 6];
B = {'a', 'b', 'c', 'e', 'f', 'ba', 'bd' , 'bc'};
tab = table(A',B');
% Sort by doubles
Output = sortrows(tab,1)
% Sort by string
Output = sortrows(tab,2)

  0 Comments

Sign in to comment.