categorical of numbers to a numerical array
16 ビュー (過去 30 日間)
古いコメントを表示
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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Categorical Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!