Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
is it related to type casting?!

Subject: is it related to type casting?!

From: Ali

Date: 14 Sep, 2010 12:42:08

Message: 1 of 7

Hi,

Suppose that "im" is a gray scale image.

I wrote this code in the command window and i reached to a strange result, see:

K>> z = im(75,140)
z =
248

K>> floor(z/16)
ans =
16

K>> floor(248/16)
ans =
15

what is the cause?

As some friends of mine, i guess it is related to type casting too ...
Could you Please give me a solution?

Subject: is it related to type casting?!

From: Bruno Luong

Date: 14 Sep, 2010 13:04:15

Message: 2 of 7

"Ali " <akharin.booseh@gmail.com> wrote in message <i6nqj0$4pd$1@fred.mathworks.com>...
> Hi,
>
> Suppose that "im" is a gray scale image.
>
> I wrote this code in the command window and i reached to a strange result, see:
>
> K>> z = im(75,140)
> z =
> 248
>
> K>> floor(z/16)
> ans =
> 16


>
> K>> floor(248/16)
> ans =
> 15
>
> what is the cause?
>

If z is UINT8

So z/16 = uint8(248)/16 = uint8(round(15.5)) = uint8(16) (Matlab round the result)
So floor( z/16 ) = floor(uint8(16)) = 16

A let you the pleasure to analyze when z is double.

Bruno

Subject: is it related to type casting?!

From: Ali

Date: 14 Sep, 2010 16:42:04

Message: 3 of 7

Dear Bruno,

consider i used floor function which rounds its element to the nearest or equal integer number,
i mean floor(15.5) should be 15 not 16 !

Subject: is it related to type casting?!

From: Sean

Date: 14 Sep, 2010 16:50:21

Message: 4 of 7

"Ali " <akharin.booseh@gmail.com> wrote in message <i6o8ks$7bc$1@fred.mathworks.com>...
> Dear Bruno,
>
> consider i used floor function which rounds its element to the nearest or equal integer number,
> i mean floor(15.5) should be 15 not 16 !

Yes. But what's:
uint8(15.5)
Now remember order of operations:
floor(uint8(15.5))

Subject: is it related to type casting?!

From: Bruno Luong

Date: 14 Sep, 2010 16:53:04

Message: 5 of 7

"Ali " <akharin.booseh@gmail.com> wrote in message <i6o8ks$7bc$1@fred.mathworks.com>...
> Dear Bruno,
>
> consider i used floor function which rounds its element to the nearest or equal integer number,
> i mean floor(15.5) should be 15 not 16 !

I think you miss my explanation. z / 16 (without the FLOOR) is *16* when z is uint8(248). Please run this code, then you might see clearer:

%%
z = uint8(248)
frac = z / 16 % <- 16!!!!!! BECAUSE MATLAB ROUND IT, FRAC IS UINT8
floor(frac)

%%
zdbl = double(248)
frac = zdbl / 16 % <- 15.5 (double)
floor(frac)

% Bruno

Subject: is it related to type casting?!

From: Matt Fig

Date: 14 Sep, 2010 17:16:07

Message: 6 of 7

docsearch('integer')

The first item that comes up covers what you are seeing.

Subject: is it related to type casting?!

From: Ali

Date: 14 Sep, 2010 18:19:22

Message: 7 of 7

Dear All, thanks.
before asking this question, i executed the following code and i didn't see any logical error:
>> z= 248
z =
   248

>> z/16
ans =
   15.5000

i had forgotten that image values are all integer :(

thanks very much.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us