使用matlab从实数截取整数

发布于 2024-09-15 19:48:12 字数 220 浏览 0 评论 0原文

请任何人帮助我从实数中截断整数,而不需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-09-22 19:48:12

fix 确实可以满足您的要求。

>>fix(255/64)
    ans =
          3

也许你的代码的其他部分没有做你认为它正在做的事情。您可以发布更多代码以及您的预期和实际输出

编辑:
Imread 将值返回为 uint8,并且此类中的除法似乎执行了您不想要的舍入。在除法之前尝试将其重铸为两倍

S = double(imread('image0286.jpg'))/64

fix does do what you want.

>>fix(255/64)
    ans =
          3

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

S = double(imread('image0286.jpg'))/64
木森分化 2024-09-22 19:48:12

floor(x) 可以做到这一点,但要注意负数。

fix 也应该有效。

另请注意,imread 可能会返回 byte 数组或 int 数组,而不是浮点数。因此您的除法可能是 int-division,并自动截断,无需 fixfloor

floor(x) does this, though beware of negative numbers.

but fix should work too.

Also note that imread might return a byte array or int array rather than floats. so your division might be int-division, and automatically truncate without the need for fix or floor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文