How to convert decimals in a matrix to integers? [closed]

半腔热情 提交于 2019-12-27 02:16:34

问题


If my input matrix is:

v =

       -0.7071    0.5774    0.4082
        0.0000   -0.5774    0.8165
        0.7071    0.5774    0.4082

The output should be:

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

回答1:


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

In case this is the exact example you have.

And just in case doubles are not good enough:

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

bsxfun way, thanks to @rayryeng:

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


来源:https://stackoverflow.com/questions/32076972/how-to-convert-decimals-in-a-matrix-to-integers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!