MATLAB 中如何将分类数组(categorical)转为数值数组(numerical)?
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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)
即先将分类数组转化为字符类型,再转化为数值类型。
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!