Is there an efficient way to sort specific ranges/subsections of rows independently in a table by a particular column?
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Cris LaPierre
2019 年 2 月 1 日
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.
採用された回答
Walter Roberson
2019 年 2 月 1 日
splitapply() provided that you do not mind that disconnected regions with the same identifier code will be brought together
10 件のコメント
Walter Roberson
2020 年 3 月 2 日
splitapply will not sort the entries. Use a function that returns a cell array around the inputs that you are given.
Oh I see, you are confused by the line of code that I had given other person,
temp = splitapply(@(varargin) {sortrows(table(varargin{:}),2)}, T, G);
Their particular request was to sort values, so there had to be a sorting call for their purposes. For your purposes just leave out the sorting step
temp = splitapply(@(varargin) {table(varargin{:})}, T, G);
PS
2020 年 3 月 2 日
Thank you so much.
It seems I am doing something wrong then, because I am using the same code, I will look into my data again.
But thank you so much for the help.
その他の回答 (1 件)
Cris LaPierre
2019 年 2 月 1 日
編集済み: Cris LaPierre
2019 年 2 月 1 日
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 件のコメント
Cris LaPierre
2019 年 2 月 1 日
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);
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!