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
is just a matrix I'm taking values from. Here is the output from MATLAB:
val =
29
bucketSize =
128
pos =
0
What am I missing?
Description. x = A ./ B divides each element of A by the corresponding element of B . The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.
Description. rem( a , b ) finds the remainder after division. If b <> 0 , then rem(a,b) = a - fix(a/b)*b . If b = 0 or b = Inf or b = -Inf , then rem returns NaN .
The return value for the integer division operator is of type integer if the operands are both integers. In contrast, it returns a float value if either one of the operands is a float.
C = idivide( A , B ) divides each element of A by the corresponding element of B , rounded to the nearest integers toward zero. A and B must contain real numbers and at least one of them must belong to an integer class.
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:
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.
If you'd like to find out more about the different numeric data types in MATLAB, you can check out this documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With