为什么类型转换会增加矩阵的 row/column 大小?
Why typecasting increases matrix's row/column size?
当我写这段代码时(只是为了测试。其他类型转换也会发生这种情况):
y_quan=typecast(y_quan,'int8');
y_quan 的维度发生了翻天覆地的变化。在进行类型转换之前,它是一个 double 类型的 1X15310 矩阵。但是在这个类型转换操作之后它变成了 int8 类型的 1X122480 矩阵。
为什么会这样?任何人请解释。
我怎样才能防止这种情况发生?
How can I prevent this?
不要用typecast
,明显是函数错误
这就是您明确要求的行为。来自 Matlab documentation, which is the obvious place that someone should look when researching functionality:
typecast
is different from the MATLAB® cast
function in that it does not alter the input data. typecast
always returns the same number of bytes in the output Y as were in the input X. For example, casting the 16-bit integer 1000 to uint8 with typecast returns the full 16 bits in two 8-bit segments (3 and 232) thus keeping its original value (3*256 + 232 = 1000). The cast function, on the other hand, truncates the input value to 255.
这意味着每个元素有 64 位的 double
矩阵有 15310 * 8 字节的存储空间。现在,将其类型转换为 8 位(==1 字节)值,结果的元素数量是原来的 8 倍。
当我写这段代码时(只是为了测试。其他类型转换也会发生这种情况):
y_quan=typecast(y_quan,'int8');
y_quan 的维度发生了翻天覆地的变化。在进行类型转换之前,它是一个 double 类型的 1X15310 矩阵。但是在这个类型转换操作之后它变成了 int8 类型的 1X122480 矩阵。 为什么会这样?任何人请解释。 我怎样才能防止这种情况发生?
How can I prevent this?
不要用typecast
,明显是函数错误
这就是您明确要求的行为。来自 Matlab documentation, which is the obvious place that someone should look when researching functionality:
typecast
is different from the MATLAB®cast
function in that it does not alter the input data.typecast
always returns the same number of bytes in the output Y as were in the input X. For example, casting the 16-bit integer 1000 to uint8 with typecast returns the full 16 bits in two 8-bit segments (3 and 232) thus keeping its original value (3*256 + 232 = 1000). The cast function, on the other hand, truncates the input value to 255.
这意味着每个元素有 64 位的 double
矩阵有 15310 * 8 字节的存储空间。现在,将其类型转换为 8 位(==1 字节)值,结果的元素数量是原来的 8 倍。