Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB division... should 29/128 return 0?

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?

like image 423
jakev Avatar asked Sep 11 '10 02:09

jakev


People also ask

How do you do division in MATLAB?

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.

How do you know if a remainder is zero in MATLAB?

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 .

Why does division return float?

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.

Which MATLAB command should you use to get the integer part of a division?

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.


1 Answers

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.

like image 123
gnovice Avatar answered Sep 17 '22 20:09

gnovice