"categorical()" cannot convert continuous data into categorical data with an error: "Unable to create default category names. Specify category names using the CATEGORYNAMES input argument."

44 views (last 30 days)
I tried to convert numbers in double array into categorical data using "categorical()" function.
However, such an error occurs as below. What is the reason for this?
"Unable to create default category names. Specify category names using the CATEGORYNAMES input argument."
To reproduce the issue, run the code below.
a= [0.157132, 0.157132, 0.157135, 0.157135, 0.19604, 0.19604];
categorical(a)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Apr 2022
The function "categorical()" can convert continuous data into categorical data. However, this function cannot categorize two numbers if the difference between them is less than 5e-5, i.e., 0.00005. For example, the function does not categorize 1 and 1.00001 into different categories. This behavior is expected and explained well in the categorical document as below.
-----------------------------------------------
If the input array has numeric, datetime, or duration values that are too close together, then the categorical function truncates them to duplicate values. For example, categorical([1 1.00001]) truncates the second element of the input array. To create categories from numeric data, use the discretize function.
-----------------------------------------------
In your example, 0.157132 and 0.157135 are very close to each other and the difference is 3e-6 which is less than 5e-5. Therefore "categorical()" cannot classify the two numbers into two categories. As a workaround, please consider using "discretize()" as below.
categorical(discretize(a, min(a):1e-6:max(a)))
 

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!