MATLAB除法... 29/29/应该返回0吗? [英] MATLAB division... should 29/128 return 0?

查看:79
本文介绍了MATLAB除法... 29/29/应该返回0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不认为这是一个精度问题,答案应该是0.226.这是确切的代码:

I really don't think this is a precision problem, the answer should be about 0.226. Here's the exact code:

val = I(i,j)
bucketSize    
pos = val / bucketSize

I只是我从中获取值的矩阵.这是MATLAB的输出:

I is just a matrix I'm taking values from. Here is the output from MATLAB:

val =

   29

bucketSize =

   128

pos =

   0

我想念什么?

推荐答案

我的猜测是您的矩阵I是从双精度值确保MATLAB执行浮点除法而不是整数除法(这将使结果四舍五入).

My guess would be that your matrix I is pixel data loaded from an image file, which will have values that are typically unsigned 8-bit integers. As already mentioned, converting both integer values to a double precision value will ensure that MATLAB performs floating point division instead of integer division (which will round off the result).

将一个值转换为双精度是不够的:

Converting one value to double precision is insufficient:

对于所有其中一个操作数是整数数组的二进制运算 数据类型(64位整数除外),另一个是标量双精度数, MATLAB使用逐元素双精度计算运算 算术,然后将结果转换回原始整数 数据类型.

For all binary operations in which one operand is an array of integer data type (except 64-bit integers) and the other is a scalar double, MATLAB computes the operation using elementwise double-precision arithmetic, and then converts the result back to the original integer data type.

如果您想了解有关MATLAB中不同数字数据类型的更多信息,可以查看此文档.

If you'd like to find out more about the different numeric data types in MATLAB, you can check out this documentation.

这篇关于MATLAB除法... 29/29/应该返回0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆