-2

I got a problem when trying to find an element in a matrix. This is my matrix:

-106.283377231500   52.3353138528333
-106.283377140333   52.3353249450000
-106.283377238667   52.3353357275000
-106.283377487833   52.3353471300000
-106.283377541833   52.3353580896667
-106.283377776667   52.3353689166667
-106.283377591333   52.3353798870000
-106.283377810667   52.3353914036667
-106.283377711500   52.3354019898333
-106.283377595333   52.3354132866667
-106.283377653833   52.3354257965000
-106.283377829333   52.3354378235000
-106.283377616500   52.3354498565000
-106.283377619333   52.3354624370000
-106.283377568000   52.3354743758333
-106.283377423333   52.3354863070000
-106.283377382667   52.3354984161667
-106.283377054833   52.3355106328333
-106.283377120000   52.3355230306667
-106.283377177833   52.3355353523333
-106.283377172667   52.3355468450000

I know I have these values in row number 9:

-106.283377711500   52.3354019898333

And simply we should be able to find out this matter by using this command:

 data(:,2)==52.3354019898333

But this gives me a zero array! The strange matter is that I can find the first element simply by the same command:

data(:,1)==-106.2833777115

And also I can get the number I'm looking for by this command:

data(9,2)

What am I doing wrong? Or is this a bug?

Sardar Usama
  • 19,328
  • 9
  • 35
  • 57

1 Answers1

1

When looking for real values, it's always a good idea to take into account numerical precision. If you compute data(9,2) - 52.3354019898333, Matlab will probably tell you that it is not absolutely 0. One way to achieve your goal would be to authorized a tiny difference between the actual value and the requested value :

abs(data(:,2) - 52.3354019898333) < sqrt(eps)
G.J
  • 820
  • 1
  • 6
  • 12
  • Another nonsense fact about MATLAB! Now it works but the problem is I have thousands of records to search and this will significantly affect the performance of my code. The wired thing was that it works for one number, yet not for the other! Thanks for your comment – Mechatronics May 10 '17 at 22:51
  • 4
    @Mechatronics: It's not a "nonsense fact". This is a fundamental aspect of dealing with floating point numbers regardless of whether you're using MATLAB or C or Java, etc. – gnovice May 11 '17 at 01:42
  • @gnovice I've never had this problem in C! Besides, as I mentioned earlier, I have no problem in one case, yet the other case. I like MATLAB because it makes it easier for prototyping. But when the program becomes complicated, these kind of problems become really anoying and takes much time to debug. – Mechatronics May 11 '17 at 02:19
  • 4
    @Mechatronics I suggest you try to reproduce this in C and see what happens. As for why one case works when another doesn't, some numbers are exactly representable in binary while others aren't, in the same way that you can represent 1/5 exactly in decimal as 0.2 but you cannot represent 1/3 exactly because it repeats 0.3333... – beaker May 11 '17 at 17:20
  • 1
    @Mechatronics If this problem does not exist in your computer for other programming languages then you have an infinite precision computer, with infinite amount of memory!! Even real life is finite due to quantum mechanics and the plank constant! You have a computer even more accurate *than the universe*. I just dont really think that is the case – Ander Biguri May 12 '17 at 09:04
  • @AnderBiguri maybe I've never gone with this much accuracy and this is the first time I realize this fact. But still doesn't make sense to me why this is true for some numbers but not for others as I observed in MATLAB. – Mechatronics May 12 '17 at 14:26
  • @Mechatronics If it doesnt make sense to you, then you need to read a bit more the duplicated question and theory of numerical precission, because it is very very logical why it works for some numbers and not others. – Ander Biguri May 12 '17 at 14:28
  • @AnderBiguri there are always two group of people in online forums. One answer the question and refer to links for better understanding, while the second persist to say they are right and every question is stupid to ask! No offense but seems you're amongst the second group! I asked a question because it took one week for me to realize what's wrong with my program with over 2000 lines of codes. If I knew about this, I would never ask right? I got my answer and this topic is closed for me. Thanks guys especially G.J for spending time and explaining. – Mechatronics May 12 '17 at 14:37
  • @Mechatronics Stackoverflow has a very stric set of rules of what is valid. Your question is valid, I have nothing wrong against it. Now that you actually have the asnwer, if you still claim that it makes no sense, then it just means that you did not take the time to udnerstand the answers. Just pointing that out. Good luck! ;) – Ander Biguri May 12 '17 at 14:40