How to sortrows by specific strings

47 次查看(过去 30 天)
I am making, an attendance sheet for a college club meeting. I have a table filled with names (string) and would like to sort them by their position (string).
I want to sort the list of people who sign in at the end of the meeting by their held "position."
1. Officers would appear at the top of the list
2. Members next
3. Guests last
However I am struggling to use sort or sortrows to do this, so there must be other commands that I could try. I provided example code and a desired output to better illustrate my question.
For example
% Create Data
Position = {'Vice President';'Guest';'Member';'President'};
Name = {'John';'Jane';'Nancy';'Bob'};
% Turn data into a table
data = table(Position,Name);
% Sort names by position:
% President->VP->Member->Guest
%%%% Your Helpful Advice would go here :) %%%%
Desired Output
%{
data =
4×2 table
Position Name
__________________ _________
{'President' } {'John' }
{'Vice President'} {'Jane' }
{'Member' } {'Nancy'}
{'Guest' } {'Bob' }
%}
Any suggestions are appreciated, thank you!
Updated to make question easier to understand for others.

采纳的回答

Steven Lord
Steven Lord 2021-3-10
desired_order = {'President','Vice President','Member','Guest'};
Position = {'Vice President';'Guest';'Member';'President'};
Name = {'John';'Jane';'Nancy';'Bob'};
Turn the Position variable into a categorical array whose order matters (the array is an ordinal array.) The ordering is given by the desired_order list.
ranks = categorical(Position, desired_order, 'ordinal', true)
ranks = 4×1 categorical array
Vice President Guest Member President
Store the data in a table array.
results = table(ranks, Name, 'VariableNames', ["Rank", "Name"])
results = 4x2 table
Rank Name ______________ _________ Vice President {'John' } Guest {'Jane' } Member {'Nancy'} President {'Bob' }
Sort the table by Rank (taking advantage of the fact that it is ordinal.)
results = sortrows(results, "Rank")
results = 4x2 table
Rank Name ______________ _________ President {'Bob' } Vice President {'John' } Member {'Nancy'} Guest {'Jane' }
Who's the Member?
results{results.Rank == 'Member', 'Name'}
ans = 1x1 cell array
{'Nancy'}
  2 个评论
David Rodriguez
David Rodriguez 2021-3-10
编辑:David Rodriguez 2021-3-10
Ahh, I did not know about categorical, I will certainly read the documentation on it.
Thank you so much for your time and help!! This was a smaller version of what I am dealing with and your code runs about 25-30% faster with this small amount of data, so I bet when I implement it into my actual data set, it will be much faster. And thank you for explaining the code, with output, really helpful!

请先登录,再进行评论。

更多回答(1 个)

David Rodriguez
David Rodriguez 2021-3-9
编辑:David Rodriguez 2021-3-10
After working on this much longer, this is what I came up with, but am wondering if there is a better way of solving the problem.
%% Sort Rows by President->Vp->Member->Guest
% turn data into cells
data = table2cell(data);
% Create new column
new_col = zeros(length(data),1);
% Write the desired order
desired_order = {'President','Vice President','Member','Guest'};
n_positions = length(desired_order);
% Assign ranking to each position
%{
P - 1
VP - 2
M - 3
G - 4
%}
% Create a "Ranking" vector for each position
ranking = 1:n_positions;
% Assign appropiate value in the new column
for aa = 1:n_positions
position = desired_order(aa);
position_check = find(strcmp(data(:,1),desired_order(aa)));
new_col(position_check) = ranking(aa);
end
% add col, sort the data, remove that col
data = addvars(cell2table(data),new_col);
data = sortrows(data,'new_col','ascend');
data = removevars(data,width(data));

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by