MATLAB四舍五入到最接近的整数

问题描述:

我有 1x50000 大小矩阵 v 我希望将其转换为零均值和单位差异:

I have a 1x50000 size matrix v and I want to convert it to zero mean and unit variance:

x = ((v-mean(v))/std2(v));

但是MATLAB没有给出精确的浮点值,而是将其转换为最接近的整数。请帮助我获取确切的值。

But instead of giving me exact floating point values MATLAB is converting it to nearest integers. Please help me in getting the exact values.

检查 v 的数据类型。我确定这是一个整数类型,使用整数运算,这就是为什么结果是整数。您需要将其转换为浮点类型对其执行浮点运算:

Check the data type for v. I'm sure it's an integer type, using integer arithmetic, which is why the result is an integer. You need to convert it to a floating point type to perform floating point operations on it:

v = double(v);              % Convert v to a double-precision float
x = ((v-mean(v))/std2(v));  % Result is now a double as well