0

I just discovered a very very odd thing: "find" doesn't find anything in MATLAB 7.8.0 (R2009a) when using "random" function!!! Does anyone know the reason? For example:

b=random('unif',1,3,1,4)

b =

1.2978    2.7994    1.9008    1.4113

find(b==2.7994)

ans =

Empty matrix: 1-by-0

As you can see the second element is 2.7994.

The interesting point is that when I manually defined b as follows, "find" could produce correct ouput:

b=[1.2978 2.7994 1.9008 1.4113]

b =

1.2978    2.7994    1.9008    1.4113

find(b==2.7994)

ans =

 2

Do you know why?

Thanks.

Amin
  • 3
  • 1
  • Welcome to the world of floating point arithmetic. You're not the first (and sadly not the last...) person to experience this nightmare. Check out the above linked duplicate for more details. – rayryeng Mar 04 '15 at 19:12

1 Answers1

1

It is because when you display values in b = random('unif',1,3,1,4) they are only displayed on screen with fewer digits than in reality ... see format command to display more digits ...

Anyway you'll never be able to display all digits, your only option is to find values up to some tolerance:

find(abs(b - 2.7994) <= 0.0001)
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56