Is there an efficient way to sort specific ranges/subsections of rows independently in a table by a particular column?
Show older comments
I want to sort sets of rows independently within a table e.g. a table such as
A 5
C 3
D 1
B 4
E 2
B 3
C 1
A 2
such that the first 5 rows and the last 3 rows are sorted separately in ascending order by column 2 so that the resulting table produced would be
D 1
E 2
C 3
B 4
A 5
C 1
A 2
B 3
Using nested loops is highly inefficient and I've been unsuccessful using sortrows(). Thanks for any suggestions.
2 Comments
Cris LaPierre
on 1 Feb 2019
Is there any sort of logic to where the splits occur? Based on what you've stated, it seems like the dividing was done arbitrarily. If you can articulate the criteria, we might be able to help come up with a solution.
Mark Bodner
on 1 Feb 2019
Edited: Walter Roberson
on 1 Feb 2019
Accepted Answer
More Answers (1)
Cris LaPierre
on 1 Feb 2019
Edited: Cris LaPierre
on 1 Feb 2019
0 votes
Took a stab. Here are my assumptions
- Groups do not overlap
- There are the same number of elements in each group with the exception of the final group
These allow me to create a new variable to represent the groups. I can then use sortrows to use the new group variable for the primary sort and your numeric variable for the secondary.
% Create your table
var1 = categorical(["A","C","D","B","E","B","C","A"])';
var2 = [5 3 1 4 2 3 1 2]';
T = table(var1,var2)
% Determine number of elements in a group and the number of groups in the table
cnt = length(unique(T.var1))
grps = ceil(height(T)/cnt)
% Create the grouping variable.
% Start by making an array with a row for each element in a group, and a column for each group
var3 = ones(cnt,1)*[1:grps];
% Use linear indexing to convert the array to a column vector
var3 = var3(:);
% Before adding to the table, make column same height as table
var3 = var3(1:height(T))
% Add new grouping variable to the table
T=addvars(T,var3)
% Sort using grouping variable as primary sort (3), and 2nd column for secondary sort(2)
T = sortrows(T,[3 2])
T =
'D' 1 1
'E' 2 1
'C' 3 1
'B' 4 1
'A' 5 1
'C' 1 2
'A' 2 2
'B' 3 2
3 Comments
Mark Bodner
on 1 Feb 2019
Mark Bodner
on 1 Feb 2019
Cris LaPierre
on 1 Feb 2019
Assuming the same number of members in each group was an assumption I made to help create the grouping variable. If your data has a grouping variable then there can be a different number in each group.
With the dataset you shared, I'd do the following
opts = spreadsheetImportOptions('NumVariables',3);
opts = setvaropts(opts,3,'TreatAsMissing','<missing>'); % C232
opts = setvartype(opts,[1 2],'categorical');
opts = setvartype(opts,3,'double');
data = readtable('sample.xlsx',opts,'ReadVariableNames',false);
dataSorted = sortrows(data,[1,3]);
Note that the groups are placed in alphabetical order. Not sure if one of your design criteria was that they don't change order. If so, you can add the following after the readtable command but before the sortrows to preserve the order of the PIN numbers.
[~,ic,~] = unique(data.Var1);
data.Var1 = categorical(data.Var1,data.Var1(sort(ic)),'Ordinal',true);
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!