09-23-2014 05:10 PM - edited 09-23-2014 05:10 PM
i have these values
35.2 |
35.3 |
35.4 |
35.5 |
35.7 |
35.8 |
35.9 36.0 |
i am looking for indexes closet 35.74 in my array. (which is index 4.)
i used the "round to nearest" function and it gave me 36.
which is index 7.
what other ways to get the right index?
Solved! Go to Solution.
09-23-2014 05:13 PM - edited 09-23-2014 05:16 PM
?
I guess you can just take the absolute value of the difference and the smallest one is the closest one?
I actually have no idea what you are trying to do.
09-23-2014 05:34 PM
i want to find
the index closest to
value 35.74
just by looking at it. it should be 35.7 or 35.8
i tried using the round to nearest function and it rounded out to be 36.00
how can i make it so that i can get 35.7?
09-23-2014 05:55 PM - edited 09-23-2014 06:05 PM
Try Threshold 1D Array.
It should be noted that you'll need a sorted array as in your example. If that's just a simple explanation for a larger context, you may need something more sophisticated.
09-23-2014 06:02 PM
You will always have some difficulties with rounding to a specific number of digits after the decimal point because of the way numbers are represented in binary. When expressed in binary 0.1 is an infinitely repeating expression. Regardless of the display the internal representation of the number always uses the maximum resolution for the data type (8 bytes for DBL).
So you need to be very careful to define exactly what you want. If you have an array of values (regardless of the way they are displayed) Then you can find the closest to a Test Value by subtracting Test Value from the array and finding the minimum of the absolute values of the differences.
Note that in the image below the same values are in Array and Array 2 with the display on Array 2 set for 20 siginficant digits.
Lynn
09-23-2014 06:37 PM
thanks!!
both method works!!
thank you, but my data is not sorted so it wont work. but the example worked.
thank you!
09-23-2014 09:00 PM
Really I should get an honorable mention (i.e., kudo) for being the first one to mention the method you chose as the solution. 😉
09-24-2014 10:17 AM - edited 09-24-2014 10:17 AM
lol.
THANK YOU BOTH
09-24-2014 10:38 AM
See Here Also
Obviously, X should be a Float but I was too lazy to fix it
09-24-2014 10:45 AM
One other comment about rounding. LabVIEW uses banker's rounding which takes any value that is dead on 0.5 and will round it to the nearest even integer. Generally in school we would have learned that 0.5 would be rounded up.
So nearest even integer may or may not be what you are looking for.