The nominal
and ordinal
array data types are not recommended. To represent ordered and unordered discrete, nonnumeric
data, use the Categorical Arrays (MATLAB) data type instead.
It is often useful to index and search data by its category, or group. If you
store categories as labels inside a cell array of character vectors or
char
array, it can be difficult to index and search the
categories. When using nominal or ordinal arrays, you can easily:
Index elements from particular
categories. For both nominal and ordinal arrays, you can
use the logical operators ==
and
~=
to index the observations that are in, or not
in, a particular category. For ordinal arrays, which have an encoded
order, you can also use inequalities, >
,
>=
, <
, and
<=
, to find observations in categories above
or below a particular category.
Search for members of a category. In
addition to the logical operator ==
, you can use
ismember
to find observations in a particular
group.
Find elements that are not in a defined
category. Nominal and ordinal arrays indicate which
elements do not belong to a defined category by
<undefined>
. You can use
isundefined
to find observations missing a
category.
Delete observations that are in a particular
category. You can use logical operators to include or
exclude observations from particular categories. Even if you remove all
observations from a category, the category level remains defined unless
you remove it using droplevels
.
This example shows several common indexing and searching methods.
Load the sample data.
load carsmall;
Convert the char
array, Origin
, to a nominal array. This variable contains the country of origin, or manufacture, for each sample car.
Origin = nominal(Origin);
Search for observations in a category. Determine if there are any cars in the sample that were manufactured in Canada.
any(Origin=='Canada')
ans = logical
0
There are no sample cars manufactured in Canada.
List the countries that are levels of Origin
.
getlevels(Origin)
ans = 1x6 nominal
France Germany Italy Japan Sweden USA
Index elements that are in a particular category. Plot a histogram of the acceleration measurements for cars made in the U.S.
figure(); histogram(Acceleration(Origin=='USA')) title('Acceleration of Cars Made in the USA')
Delete observations that are in a particular category. Delete all cars made in Sweden from Origin
.
Origin = Origin(Origin~='Sweden'); any(ismember(Origin,'Sweden'))
ans = logical
0
The cars made in Sweden are deleted from Origin
, but Sweden
is still a level of Origin
.
getlevels(Origin)
ans = 1x6 nominal
France Germany Italy Japan Sweden USA
Remove Sweden
from the levels of Origin
.
Origin = droplevels(Origin,'Sweden');
getlevels(Origin)
ans = 1x5 nominal
France Germany Italy Japan USA
Check for observations not in a defined category. Get the indices for the cars made in France.
ix = find(Origin=='France')
ix = 4×1
11
27
39
61
There are four cars from France. Remove France
from the levels of Origin
.
Origin = droplevels(Origin,'France');
This returns a warning indicating that you are dropping a category level that has elements in it. These observations are no longer in a defined category, indicated by undefined
.
Origin(ix)
ans = 4x1 nominal
<undefined>
<undefined>
<undefined>
<undefined>
You can use isundefined
to search for observations with an undefined category.
find(isundefined(Origin))
ans = 4×1
11
27
39
61
These indices correspond to the observations that were in category France
, before that category was dropped from Origin
.
droplevels
| nominal
| ordinal