使用matlab从实数截取整数
请任何人帮助我从实数中截断整数,而不需要在 matlab 中舍入到最接近的整数,例如:如果我有 255/64=3.98,我只需要 3 而不是 4。我在程序中使用了修复,但不起作用。我的代码是:
S=imread('image0286.jpg')/64;
disp(fix(S);
这给了我将 S 的元素四舍五入到最接近的整数后的输出,而不是削减整数。
please, any one help me to trunct the integer number from real number without any round to nearest integers in matlab ,e.g: if i have 255/64=3.98 I need just 3 not 4. I used fix in my program but not work. my cod is:
S=imread('image0286.jpg')/64;
disp(fix(S);
this give me the output after rounds the elements of S to the nearest integers not cut the integer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fix
确实可以满足您的要求。也许你的代码的其他部分没有做你认为它正在做的事情。您可以发布更多代码以及您的预期和实际输出
编辑:
Imread 将值返回为 uint8,并且此类中的除法似乎执行了您不想要的舍入。在除法之前尝试将其重铸为两倍
fix
does do what you want.maybe some other part of your code isn't doing what you think it is doing. could you post more code and your expected and real output
EDIT:
Imread returns the values as uint8, and division in this class seems to carry out the rounding you don't want. try recasting to double before dividing
floor(x)
可以做到这一点,但要注意负数。但
fix
也应该有效。另请注意,
imread
可能会返回byte
数组或int
数组,而不是浮点数。因此您的除法可能是 int-division,并自动截断,无需fix
或floor
。floor(x)
does this, though beware of negative numbers.but
fix
should work too.Also note that
imread
might return abyte
array orint
array rather than floats. so your division might be int-division, and automatically truncate without the need forfix
orfloor
.