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

29 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 31 de Oct. de 2019
Respondida: MathWorks Support Team el 31 de Oct. de 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
这个结果并不正确。

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre 分类数组 en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!