MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Columns 7 through 8

medium small

class(sizeOrd)

ans =
'categorical'

The order of the values in the categorical array, sizeOrd, remains unchanged.

List the discrete categories in the categorical variable, sizeOrd.

categories(sizeOrd)

ans = 3x1 cell array
{'small' }
{'medium'}
{'large' }

The categories are listed in the specified order to match the mathematical ordering
small < medium < large.

Create Ordinal Categorical Array by Binning Numeric Data

Create a vector of 100 random numbers between zero and 50.

x = rand(100,1)*50;

Use the discretize function to create a categorical array by binning the values of x.
Put all values between zero and 15 in the first bin, all the values between 15 and 35 in the
second bin, and all the values between 35 and 50 in the third bin. Each bin includes the
left endpoint, but does not include the right endpoint.

catnames = {'small','medium','large'};
binnedData = discretize(x,[0 15 35 50],'categorical',catnames);

binnedData is a 100-by-1 ordinal categorical array with three categories, such that
small < medium < large.

Use the summary function to print the number of elements in each category.

summary(binnedData)

8 Categorical Arrays

Free download pdf