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:
IF STATEMENT MATCHING NUMBERS

Subject: IF STATEMENT MATCHING NUMBERS

From: ALEX

Date: 15 Jul, 2013 17:39:07

Message: 1 of 3

I'M BAFFLED BY THIS, I HAVE ROUNDED TWO SEPARATE COLUMNS FROM TWO DIFFERENT MATRICES. I WANT TO MATCH AN ELEMENT FROM ONE COLUMN WITH AN ELEMENT FROM A DIFFERENT COLUMN. THESE ARE NUMBERS WHICH HAVE BEEN ROUNDED TO THE TENTH. FOR SOME REASON THE IF STATEMENT ONLY ADDRESSES HAVE OF THE NUMBERS WHICH ARE IDENTICAL. NOT SURE WHY IT IS DOING THIS. ANY IDEAS?

FOR BACKGROUND, I WANT TO MATCH THE TIMESTAMP FROM TWO DIFFERENT SETS OF DATA. ONE DATA SET IS AT 1/10 SEC INTERVALS, THE OTHER IS 2/10 SEC INTERVALS.

THANKS SO MUCH

Subject: IF STATEMENT MATCHING NUMBERS

From: someone

Date: 15 Jul, 2013 17:50:09

Message: 2 of 3

"ALEX" wrote in message <ks1c3q$jg9$1@newscl01ah.mathworks.com>...
> I'M BAFFLED BY THIS, I HAVE ROUNDED TWO SEPARATE COLUMNS FROM TWO DIFFERENT MATRICES. I WANT TO MATCH AN ELEMENT FROM ONE COLUMN WITH AN ELEMENT FROM A DIFFERENT COLUMN. THESE ARE NUMBERS WHICH HAVE BEEN ROUNDED TO THE TENTH. FOR SOME REASON THE IF STATEMENT ONLY ADDRESSES HAVE OF THE NUMBERS WHICH ARE IDENTICAL. NOT SURE WHY IT IS DOING THIS. ANY IDEAS?
>
> FOR BACKGROUND, I WANT TO MATCH THE TIMESTAMP FROM TWO DIFFERENT SETS OF DATA. ONE DATA SET IS AT 1/10 SEC INTERVALS, THE OTHER IS 2/10 SEC INTERVALS.
>
> THANKS SO MUCH

No need to shout!

Even though the numbers appear to be rounded to the tenths, MATLAB
cannot represent & store the number EXACTLY to the tenths. (Its not MATLAB's fault, thats simply a limitation of computers internally using binary numbers.)

For a more detailed explaination and a work-around see Q5.1 of the MATLAB FAQ:

<<http://matlab.wikia.com/wiki/FAQ>>

Specifically:

<<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>>

Subject: IF STATEMENT MATCHING NUMBERS

From: TideMan

Date: 15 Jul, 2013 20:25:53

Message: 3 of 3

On Tuesday, July 16, 2013 5:39:07 AM UTC+12, ALEX wrote:
> I'M BAFFLED BY THIS, I HAVE ROUNDED TWO SEPARATE COLUMNS FROM TWO DIFFERENT MATRICES. I WANT TO MATCH AN ELEMENT FROM ONE COLUMN WITH AN ELEMENT FROM A DIFFERENT COLUMN. THESE ARE NUMBERS WHICH HAVE BEEN ROUNDED TO THE TENTH. FOR SOME REASON THE IF STATEMENT ONLY ADDRESSES HAVE OF THE NUMBERS WHICH ARE IDENTICAL. NOT SURE WHY IT IS DOING THIS. ANY IDEAS?
>
>
>
> FOR BACKGROUND, I WANT TO MATCH THE TIMESTAMP FROM TWO DIFFERENT SETS OF DATA. ONE DATA SET IS AT 1/10 SEC INTERVALS, THE OTHER IS 2/10 SEC INTERVALS.
>
>
>
> THANKS SO MUCH

Are you shouting intentionally, or have you inadvertently hit the Caps Lock?

For comparison of floating point numbers, you need to replace:
if v1==v2
with
if abs(v1-v2) < tol
where tol is a small number like 1e-6.

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