categorical of numbers to a numerical array

15 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 24 Lug 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

Più risposte (0)

Categorie

Scopri di più su Categorical Arrays in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by