Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: problem with the find function in matlab

Subject: problem with the find function in matlab

From: koolguyuf

Date: 05 Jan, 2008 15:54:45

Message: 1 of 9

Hi,

I am facing some weird behavior using the find function in matlab
versions 7.0 and 7.5 . I have this extremely simple code to execute.

a = [0:0.2:2];
my_index = find(a == 0.6)

plz let me know whether you are getting my_index = 4 ? Suprisingly, I
am not able to get it.

Plz help.

Thanks
TD

Subject: Re: problem with the find function in matlab

From: Huy

Date: 05 Jan, 2008 16:05:18

Message: 2 of 9

koolguyuf <tdgoswami@gmail.com> wrote in message <bf28c7fc-
c160-40da-ac3b-0e7b48b025b1@v46g2000hsv.googlegroups.com>...
> Hi,
>
> I am facing some weird behavior using the find function in
matlab
> versions 7.0 and 7.5 . I have this extremely simple code
to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ?
Suprisingly, I
> am not able to get it.
>
> Plz help.
>
> Thanks
> TD

That's right.

The first command will create a vector a

a = [0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0]

The second line returns my_index = 4 because a(4) = 0.6.

Anh Huy Phan
RIKEN - BSI



Subject: Re: problem with the find function in matlab

From: Shaun

Date: 05 Jan, 2008 16:05:59

Message: 3 of 9

>> format hex
>> a = [0:0.2:2];
>> a(4)
ans =
   3fe3333333333334
>> 0.6
ans =
   3fe3333333333333

Look into floating point numbers. There are several posts

Subject: Re: problem with the find function in matlab

From: Huy

Date: 05 Jan, 2008 16:11:12

Message: 4 of 9

koolguyuf <tdgoswami@gmail.com> wrote in message <bf28c7fc-
c160-40da-ac3b-0e7b48b025b1@v46g2000hsv.googlegroups.com>...
> Hi,
>
> I am facing some weird behavior using the find function in
matlab
> versions 7.0 and 7.5 . I have this extremely simple code
to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ?
Suprisingly, I
> am not able to get it.
>
> Plz help.
>
> Thanks
> TD

That's right.

The first command will create a vector a

a = [0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0]

The second line returns my_index = 4 because a(4) = 0.6.

Anh Huy Phan
RIKEN - BSI



Subject: Re: problem with the find function in matlab

From: dpb

Date: 05 Jan, 2008 16:23:02

Message: 5 of 9

koolguyuf wrote:
> Hi,
>
> I am facing some weird behavior using the find function in matlab
> versions 7.0 and 7.5 . I have this extremely simple code to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ? Suprisingly, I
> am not able to get it.

W/ R12 it returns 4.

What does it return for you?

What platform?

If not 4, one would have to presume there's an issue of exact matching
for the particular combination of OS/version/hardware.

To get to the bottom of behavior, need above information at least.
Executing the lines and pasting results could be useful as could

  a = [0:0.2:2];
  b = a - 0.6

just possibly be educational...

--

Subject: Re: problem with the find function in matlab

From: Roger Stafford

Date: 05 Jan, 2008 20:52:05

Message: 6 of 9

koolguyuf <tdgoswami@gmail.com> wrote in message <bf28c7fc-
c160-40da-ac3b-0e7b48b025b1@v46g2000hsv.googlegroups.com>...
> Hi,
>
> I am facing some weird behavior using the find function in matlab
> versions 7.0 and 7.5 . I have this extremely simple code to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ? Suprisingly, I
> am not able to get it.
>
> Plz help.
>
> Thanks
> TD
---------
  The problem is not with the 'find' command but with the floating point
representation of 0.2 in a. The n-th term in a vector expression, [p:d:q],
should ideally be p+(n-1)*d. However, the difficulty is that this latter
computation may experience round-off errors due to the imprecision of the
computer's representation of such a d. After all, it is using binary not
decimal floating point numbers. Using format hex with your example we get

 0+(4-1)*.2 = 3fe3333333333334

whereas the most accurate representation of .6 is actually

 .6 = 3fe3333333333333

which is just slightly different in the least bit (as Shaun has also pointed out.)
When you write find(a==.6), the find command requires exact equality of
both sides of the equation and would therefore not pick up the fourth index.

  MathWorks uses various schemes in computing progressions of the form
[p:d:q] to avoid some of these eccentric results, but it is unlikely they are able
to avoid all of them. If you have a value of d which has an exact expression
in decimal but not in binary, it is likely that a long enough sequence will
encounter cases that would be missed by a 'find' scan, even in their latest
matlab versions.

Roger Stafford

Subject: Re: problem with the find function in matlab

From: Jerecho

Date: 06 Jan, 2008 03:08:54

Message: 7 of 9

koolguyuf <tdgoswami@gmail.com> wrote in message <bf28c7fc-
c160-40da-ac3b-0e7b48b025b1@v46g2000hsv.googlegroups.com>...
> Hi,
>
> I am facing some weird behavior using the find function
in matlab
> versions 7.0 and 7.5 . I have this extremely simple code
to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ?
Suprisingly, I
> am not able to get it.
>
> Plz help.
>
> Thanks
> TD

Hi,

Im also experiencing the same problem. But when I
entered 'a' manually (i.e. a = [0 0.1 0.2 0.4 0.6 ... 2])
I get the correct answer.

Subject: Re: problem with the find function in matlab

From: Roger Stafford

Date: 06 Jan, 2008 03:46:36

Message: 8 of 9

"Jerecho " <jeric3182@mathworks.com> wrote in message <flpgo6$ouf
$1@fred.mathworks.com>...
> Im also experiencing the same problem. But when I
> entered 'a' manually (i.e. a = [0 0.1 0.2 0.4 0.6 ... 2])
> I get the correct answer.
-------
That should tell you what the trouble is, Jerecho. It's not the fault of 'find'. It's
because 0:.2:2 generates a slightly different vector of values than [0,.2,.4,.6,
etc]. And that in turn is because it does not have an exact value of .2 to work
with. It cannot achieve .2 precisely and any calculations with it are bound to go
slightly wrong at some stage.

Roger Stafford


Subject: Re: problem with the find function in matlab

From: Steven Lord

Date: 07 Jan, 2008 00:43:51

Message: 9 of 9


"koolguyuf" <tdgoswami@gmail.com> wrote in message
news:bf28c7fc-c160-40da-ac3b-0e7b48b025b1@v46g2000hsv.googlegroups.com...
> Hi,
>
> I am facing some weird behavior using the find function in matlab
> versions 7.0 and 7.5 . I have this extremely simple code to execute.
>
> a = [0:0.2:2];
> my_index = find(a == 0.6)
>
> plz let me know whether you are getting my_index = 4 ? Suprisingly, I
> am not able to get it.

This is a very frequently asked question.

http://matlabwiki.mathworks.com/MATLAB_FAQ

Question 6.1 talks about this type of behavior.

--
Steve Lord
slord@mathworks.com


Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics