categorical of numbers to a numerical array

16 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2018 年 7 月 24 日
回答済み: MathWorks Support Team 2018 年 7 月 24 日
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

採用された回答

MathWorks Support Team
MathWorks Support Team 2018 年 7 月 24 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCategorical Arrays についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by