How to sortrows by specific strings

조회 수: 47 (최근 30일)
David Rodriguez
David Rodriguez 2021년 3월 9일
편집: David Rodriguez 2023년 1월 14일
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!
Thien-Minh
Thien-Minh 2022년 11월 20일
perfect!

댓글을 달려면 로그인하십시오.

추가 답변 (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