如何将矩阵中的小数转换为整数?

时间:2015-08-18 15:46:56

标签: matlab matrix

如果我的输入矩阵是:

v =

       -0.7071    0.5774    0.4082
        0.0000   -0.5774    0.8165
        0.7071    0.5774    0.4082

输出应为:

v =
       -1         1         1
        0        -1         2
        1         1         1

1 个答案:

答案 0 :(得分:2)

[A,B] = size(v)
for ii = 1:B
    v(:,ii) = v(:,ii)./abs(min(v(:,ii)));
end

如果这是您的确切示例。

以防双打不够好:

v = int32(v); % or whichever integer you want

bsxfun方式,感谢@rayryeng:

v = bsxfun(@rdivide, v, abs(min(v,1)));