views:

151

answers:

4

I really don't think this is a precision problem, the answer is .226-ish... 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?

A: 

These variables are probably ints rather than doubles or longs. Does 1/2 return .5? Do other operations work?

gary comtois
Yes. For instance, 29/128 results in the correct answer. I was under the impression Matlab was typeless? How do I set the variables to be doubles?
JakeVA
+2  A: 

try:

double(val)/double(bucketSize)
AmirW
this would have worked too, thanks
JakeVA
+1  A: 

I got it, the problem is that my matrix for some reason contained uint8's, not doubles. Just changed val=I(i,j) to val=double( I(i,j) ) and all is well. Thanks.

JakeVA
+4  A: 

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 one or 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).

If you'd like to find out more about the different data types (i.e. classes) in MATLAB, you can check out this documentation.

gnovice

related questions