categorical of numbers to a numerical array

15 Ansichten (letzte 30 Tage)
I have a categorical with categories that are integers. How do I convert my categorical to a numerical array? When I call "double" it is giving an array with different numbers than my original numbering.
>> c = categorical(["5","3","2","1","8", "8", "2", "1"]);
>> d = double(c)
d =
4 3 2 1 5 5 2 1

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 24 Jul. 2018
Categoricals do not use ordering in the same way as arrays or other data types.
categories(c)
ans =
5×1 cell array
{'1'}
{'2'}
{'3'}
{'5'}
{'8'}
Calling double on a categorical is just giving the ordering (alphabetically or numerically) of the values. So the lowest number (or earliest in the alphabet) is being defined as 1, then the second lowest is defined as 2 and so on.
Given the categorical above, 5 is the fourth highest so it becomes 4 and so on...
5 -> 4
3 -> 3
2 -> 2
1 -> 1
8 -> 5
To get these numbers into a double, you can first convert the categorical to a string and then to a double.
>> s= string(c);
>> d = double(s)
d =
5 3 2 1 8 8 2 1

Weitere Antworten (0)

Kategorien

Mehr zu Categorical Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by