How to sort a table by category within column

조회 수: 23 (최근 30일)
Kylie Hansen
Kylie Hansen 2018년 2월 1일
댓글: Walter Roberson 2020년 4월 6일
I have a table with 11 columns and 59 unnamed rows. I can sort the rows alphabetically, ascending, descending, increasing, decreasing, etc. by using
Sort_Table = sortrows(Table_Name,'variable_name');
How can I sort a table by a category within a column in a way that is not alphabetical? For example, the column is a list of countries (so that several rows have the same country name) and I wish to list first the rows with a certain country that is neither alphabetically first or last. I've tried
Sort_Table = sortrows(Table_Name,'variable_name','variable_value');
as well as
Sort_Table = sortrows(Table_Name,'variable_name',{variable_value});
but neither provided the results I desired. Some help would be appreciated!
  댓글 수: 2
phillip kataswa
phillip kataswa 2020년 4월 6일
doorsorted = [];
for h = 1:length(door)
if numberOfDoors(h) == "2"
doorsorted = [doorsorted2];
elseif numberOfDoors(h) == "3"
doorsorted = [doorsorted3];
elseif numberOfDoors(h) == "4"
doorsorted = [doorsorted4];
elseif numberOfDoors(h) == "5more"
doorsorted = [doorsorted5];
end
end
what does this code mean
Walter Roberson
Walter Roberson 2020년 4월 6일
numberOfDoors is expected to be a string() array with at least as many entries as the maximum dimension of the variable doors (unless doors is empty on any dimension.)
You start at the beginning of the array numberOfDoors, and compare each entry in turn with the strings "2", "3", "4", or "5more". If it is "2" you replace the entire content of the variable doorsorted with the contents of the variable doorsorted2. If it is "3" instead then you replace the entire content of the variable doorsorted with the contents of the variable doorsorted3, and likewise "4" -> doorsorted4, and "5more" -> doorsorted5. Then you go on to the next entry in numberOfDoors, probably overwriting the entire variable doorsorted when you do.
With all of the overwriting going on, the net effect is:
  • if door is empty, or if none of the entries in numberOfDoors match "2", "3", "4", or "5more", then doorsorted will be left as the empty array door
  • otherwise, find the last entry in numberofDoors that matches one of "2", "3", "4", or "5more", and assign the corresponding variable doorsorted2, doorsorted3, doorsorted4, or doorsorted5 to doorsorted.
The code might well be wrong. I would tend to expect instead
doorsorted = [];
for h = 1:length(door)
if numberOfDoors(h) == "2"
doorsorted(h) = doorsorted2;
elseif numberOfDoors(h) == "3"
doorsorted(h) = doorsorted3;
elseif numberOfDoors(h) == "4"
doorsorted(h) = doorsorted4;
elseif numberOfDoors(h) == "5more"
doorsorted(h) = doorsorted5;
else
doorsorted(h) = nan; %or 0 or inf or "other" or something like that
end
end

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 1일
  댓글 수: 2
Peter Perkins
Peter Perkins 2018년 2월 3일
Yes, as Walter suggests, you should convert your countries data to a categorical variable, and define whatever order you want them in. You could make the categorical variable ordinal, but even a non-ordinal categorical has a (non-mathematical) ordering that's useful for display and sorting.
Kylie Hansen
Kylie Hansen 2018년 2월 6일
Thank you very much, both of you! I had never of heard of a categorical variable before, but I feel like I can make use of it quite often in the future.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by