Add and Drop Category Levels
This example shows how to add and drop levels from a nominal or ordinal array.
Note
The nominal
and ordinal
array data types are not recommended. To represent ordered and unordered discrete, nonnumeric
data, use the Categorical Arrays data type instead.
Load sample data.
load('examgrades')
The array grades
contains exam scores from 0 to 100 on five
exams for a sample of 120 students.
Create an ordinal array.
Assign letter grades to each student for each test using these categories.
Grade Range | Letter Grade |
---|---|
100 | A+ |
90–99 | A |
80–89 | B |
70–79 | C |
60–69 | D |
letter = ordinal(grades,{'D','C','B','A','A+'},[],... [60,70,80,90,100,100]); getlevels(letter)
ans = D C B A A+
There are five grade categories, in the specified order D
< C
< B
< A
< A+
.
Check for undefined categories.
Check whether or not there are any exam scores that do not fall into the five letter categories.
any(isundefined(letter))
ans = 1 0 1 1 0
Recall that there are five exam scores for each student. The previous command
returns a logical value for each of the five exams, indicating whether there are
any scores that are <undefined>
. There are scores for
the first, third, and fourth exams that are
<undefined>
, that is, missing a category level.
Identify elements in undefined categories.
You can find the exam scores that do not have a letter grade using the
isundefined
logical condition.
grades(isundefined(letter))
ans = 55 59 58 59 54 57 56 59 59 50 59 52
The exam scores that are in the 50s do not have a letter grade.
Add a new category.
Put all scores that are <undefined>
into a new
category labeled
D-
.
letter = addlevels(letter,'D-'); letter(isundefined(letter)) = 'D-'; getlevels(letter)
ans = D C B A A+ D-
letter
, has a new category added to the
end. Reorder category levels.
Reorder the categories so that D-
<
D
.
letter = reorderlevels(letter,{'D-','D','C','B','A','A+'}); getlevels(letter)
ans = D- D C B A A+
Compare elements.
Now that all exam scores have a letter grade, count how many students received a higher letter grade on the second test than on the first test.
sum(letter(:,2) > letter(:,1))
ans = 32
Thirty-two students improved their letter grade between the first two exams.
Explore categories.
Count the number of A+
scores in each of the five
exams.
sum(letter=='A+')
ans = 0 0 0 0 0
There are no A+
scores on any of the five exams.
Drop a category.
Drop the category A+
from the ordinal variable,
letter
.
letter = droplevels(letter,'A+');
getlevels(letter)
ans = D- D C B A
A+
is no longer in the ordinal variable,
letter
.See Also
ordinal
| reorderlevels
| droplevels
Related Examples
- Create Nominal and Ordinal Arrays
- Reorder Category Levels
- Merge Category Levels
- Index and Search Using Nominal and Ordinal Arrays