Problem finding find command

1 visualizzazione (ultimi 30 giorni)
antennist
antennist il 31 Ago 2016
Commentato: Stephen23 il 1 Set 2016
Hello, I am facing this very irrating probelm using matlab find() command. I have matrix with angle values from 0 till 180 and I wanted to find a index of particular angle. I prompt user to enter angle to find and after getting input I store in a variable "x" and then using find command i.e. find(a.angle.value==x) I try to find index but its giving me empty matrix except for angles 0 and 180 degrees any other angle in between its giving me empty matrix. My angles goes from 0 till 180 with a step size of 2 degrees so there are no decimal point issue. I even tried to put with four decimal points like 20.0000 but still giving me empty matrix. Any ideas what went wrong ?
Please find below the Sample data
a.angle.value=[0 1.99999999771467 3.99999999542933 5.99999999314400 ........ 180].
  3 Commenti
Stephen23
Stephen23 il 31 Ago 2016
Modificato: Stephen23 il 31 Ago 2016
@antennist: I am guessing that you come from a culture that uses the comma for the decimal radix, and you actually meant to use the period (as is used in English):
a.angle.value = [0, 1.99999999771467, 3.99999999542933, 5.99999999314400 ........ 180]
antennist
antennist il 31 Ago 2016
yes in here we use comma as a point. My apologies I have edited my post

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 31 Ago 2016
Modificato: Stephen23 il 31 Ago 2016
Floating point numbers are how decimal values are stored in your computer: they do not have infinite precision, and they can only approximate many values. Even if it looks like the "actual" value is being shown to you this does not mean that the internal representation inside your computer is the same value.
Of course this means that every operation you perform on floating point values will accumulate these errors. It is up to you (and every other programmer) to design programs that take this "floating point error" into account.
If you actually gave us some data we could show this to you.
Read all about it:
And if you want to see what the decimal equivalent of a floating point value is, then try this FEX submission:
As its author points out: "Don't confuse the exact conversion with significance!"
EDIT after antennist added actual values to their question:
What value do you expect 1.99999999771467 to be equal to? Is 1.99999999771467 equal to 2 ? Do you expect 2==1.99999999771467 to be true ?
"Any ideas what went wrong ?"
There is nothing wrong with find. However comparing floating point numbers for equality is a very bad idea, because after calculating or even defining floating point numbers they can have small differences that are not visible when you look at them in the command window (or variable viewer, etc), but those differences mean that the values are not equal.
To compare floating point values in a robust way, then you should include a tolerance in the comparison:
>> tol = 0.1;
>> num = 4;
>> vec = [0, 1.99999999771467, 3.99999999542933, 5.99999999314400];
>> find(abs(vec-num)<tol)
ans = 3
  4 Commenti
antennist
antennist il 31 Ago 2016
@Stephen Cobeldick Thanx alot for your help and I will keep your advice in mind.
Stephen23
Stephen23 il 1 Set 2016
@antennist: I hope that you got is working. If my answer helped you to resolve your question, then please accept it, which is an easy way for you to say "thank you" to us volunteers.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by