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

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

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

I is just the matrix from which I take values. Here is the result of MATLAB:

 val = 29 bucketSize = 128 pos = 0 

What am I missing?

+6
matlab division integer-division
source share
4 answers

I assume that your matrix I is pixel data loaded from an image file that will have values ​​that are usually unsigned 8-bit integers . As already mentioned, converting two integer values ​​to a double precision value ensures that MATLAB performs floating point division instead of integer division (which will round off the result).

Converting a single value to double precision is not enough:

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

If you want to know more about the different numeric data types in MATLAB, you can check out this documentation .

+16
source share

to try:

 double(val)/double(bucketSize) 
+10
source share

I realized the problem is that for some reason my matrix contained uint8 rather than doubles. Just changed val = i (i, j) to val = double (I (i, j)), and all is well. Thanks.

+1
source share

These variables are probably ints, not double or longs. Does it return 1 / 2.5? Are other operations being performed?

0
source share

All Articles