在Matlab中将结构转换为双重类型 [英] Convert struct to double type in Matlab
问题描述
Glcm(特征提取方法)给出了'struct'类型的输出,而我需要输出'double'类型。我需要一个双类型作为下一步的输入变量。
Glcm (feature extraction method) give me an output in 'struct' type, while i need the output in 'double' type. I need a 'double' type as the input variable for the next step.
所以,我试图用下面显示的几个代码转换它。
So, I've tried to convert it using several code showed below.
[gl] = glcm (B);
[gl] = struct2cell (gl);
[gl] = cell2mat (gl);
[fetrain] = double (gl);
代码给我一个输出,但它是在'复数双'类型。
The code give me an output but it's in 'complex double' type.
有没有更好的方法来将'struct'转换为'double'键入?
Is there a better way to convert 'struct' to 'double' type?
或将复合双转换为双类型?
Or to convert 'complex double' to 'double' type?
任何帮助和建议非常感谢谢谢。
Any help and suggestion would be very appreciated. Thank you.
推荐答案
首先,而不是首先转换为单元格,然后转换为矩阵,可以直接从 struct
到 double
使用 struct2array
。
First of all, rather than first converting to a cell and then to a matrix, you can convert directly from a struct
to a double
using struct2array
.
fetrain = struct2array(gl);
除此之外,复杂 double
和 double
。它们都是类型 double
。
That aside, there is no difference between a "complex double
" and a double
. They are both of type double
.
class(1i)
% double
您可以使用真实
来获取复数的真实组件或 abs
如果你需要它的大小。
You can use real
to get the real component of the complex number or abs
if you need the magnitude of it.
real(1+1i)
% 1
abs(1+1i)
% 1.4142
在你的情况下这将是:
fetrain_real = real(fetrain);
fetrain_mag = abs(fetrain);
更新
默认情况下, struct2array
水平连接数据。如果您希望数据是 nFields x nData
的矩阵,那么您可以执行以下操作:
By default struct2array
concatenates the data horizontally. If you want your data to be a matrix that is nFields x nData
, then you could do something like the following:
fetrain = struct2array(gl);
% Reshape to be nFields x nData
fetrain = permute(reshape(fetrain, [], numel(fieldnames(gl)), numel(gl)), [2 1 3]);
这篇关于在Matlab中将结构转换为双重类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!