MATLAB 中如何将分类数组(c​ategorical​)转为数值数组(nu​merical)?

29 views (last 30 days)
例如分类数组 c的定义是:
>> c = categorical(["5","3","2","1","8","8", "2", "1"]);
当我强制将 c 转为数值数组时,
>> d = double(c)
结果为:
d =
4 3 2 1 5 5 2 1
这个结果并不正确。

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 Oct 2019
分类数组的排序和数值数组不同。当执行:
>> categories(c)
时,会获得结果:
ans =
5×1 cell array
{'1'}
{'2'}
{'3'}
{'5'}
{'8'}
使用 double 函数时,只是给出了排序,即:
5 -> 4
3 -> 3
2 -> 2
1 -> 1
8 -> 5
如果想要对内容进行数据类型变换,请执行:
>> s= string(c);
>> d = double(s)
即先将分类数组转化为字符类型,再转化为数值类型。

More Answers (0)

Categories

Find more on 分类数组 in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!