Numerical Sorting of Cell Matrix by Single Column

1 view (last 30 days)
This is the cell array I am trying to numerically sort.
CombinedRanking_unsorted =
12×4 cell array
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa0' '158.6' '9653.1008' '11'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa10' '158.6' '9388.1686' '3'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa15' '158.6' '9366.2083' '2'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa5' '158.6' '9482.5697' '7'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa0' '197.4875' '9715.4104' '13'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa10' '197.4875' '9448.7681' '6'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa15' '197.4875' '9426.6661' '5'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa5' '197.4875' '9543.7786' '10'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa0' '198.25' '9750.3424' '15'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa10' '198.25' '9482.7414' '10'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa15' '198.25' '9460.5599' '8'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa5' '198.25' '9578.0935' '12'
I am trying to sort these numerically according to the value in the 4th column. I am doing this using the "sortrows" function. My input is:
FinalRanking = sortrows(CombinedRanking_unsorted,4)
The output of this is
FinalRanking =
12×4 cell array
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa5' '197.4875' '9543.7786' '10'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa10' '198.25' '9482.7414' '10'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa0' '158.6' '9653.1008' '11'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa5' '198.25' '9578.0935' '12'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa0' '197.4875' '9715.4104' '13'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa0' '198.25' '9750.3424' '15'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa15' '158.6' '9366.2083' '2'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa10' '158.6' '9388.1686' '3'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa15' '197.4875' '9426.6661' '5'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa10' '197.4875' '9448.7681' '6'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa5' '158.6' '9482.5697' '7'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa15' '198.25' '9460.5599' '8'
What would be the best method for sorting this numerically. This is only a sample set of a larger volume of data.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 16 Mar 2018
A = { 'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa0' '158.6' '9653.1008' '11'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa10' '158.6' '9388.1686' '3'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa15' '158.6' '9366.2083' '2'
'Wt0,4_Bw8,3_Bt0,4_Ah15_Aa5' '158.6' '9482.5697' '7'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa0' '197.4875' '9715.4104' '13'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa10' '197.4875' '9448.7681' '6'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa15' '197.4875' '9426.6661' '5'
'Wt0,5_Bw8,2_Bt0,5_Ah15,5_Aa5' '197.4875' '9543.7786' '10'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa0' '198.25' '9750.3424' '15'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa10' '198.25' '9482.7414' '10'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa15' '198.25' '9460.5599' '8'
'Wt0,5_Bw8,3_Bt0,5_Ah15_Aa5' '198.25' '9578.0935' '12'}
[~,ii] = sort(str2double(A(:,4)));
out = A(ii,:);
  1 Comment
Jan
Jan on 16 Mar 2018
@Andrei: The question was "sorting this numerically" and all you do is converting the data to a numerical type, sort it and apply the sorting order to the data. Well. Trivial, but perfect. :-) +1

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!