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

34 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 31 Oct 2019
例如分类数组 c的定义是:
>> c = categorical(["5","3","2","1","8","8", "2", "1"]);
当我强制将 c 转为数值数组时,
>> d = double(c)
结果为:
d =
4 3 2 1 5 5 2 1
这个结果并不正确。

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 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)
即先将分类数组转化为字符类型,再转化为数值类型。

Plus de réponses (0)

Catégories

En savoir plus sur 分类数组 dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!