LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Search 1D Array Function

Solved!
Go to solution

Hello Gents, I have a problem with the search 1d Array Function, my complessive VI works quite fine but sometimes i have a bug that i didn't understand at first, after some investigation i found the problem in a Sub VI of my containing the Search 1D Array Function,

I have two types of problem:

1) In the first two pictures attached, you can see the function skips the first value that it is searching (the values is 0,0608477) for, takes the second value equal to that and gives me consequentially the wrong index (26 instead of 25), isn't this function supposed to go in order?

2)In the other two pictures attached You can see that even if the value searched is contained in the array, the function doesn't find it and gives a result index equal to -1, does anybody know why?

Thanks for all the answers and possible explanations, I can't attach all my VI because it's quite big and i cannot share the data i'm working with.

Download All
0 Kudos
Message 1 of 17
(4,975 Views)

Hi Bonifacio,

 

this all boils down to "NEVER compare floating point values for equality!"

 

Instead of Search1DArray (which looks for exactly the same value in the array) you should use Threshold1DArray instead (as long as your input "Array" is sorted by value)…

 


@BonifacioLupo wrote:

I can't attach all my VI because it's quite big and i cannot share the data i'm working with.


The part of your VI shown in those images is rather small!

And it would help to set the "Array" data as default data (in the context menu).

It also would help to use AutoCleanup…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 17
(4,965 Views)
Solution
Accepted by topic author BonifacioLupo

Comparing doubles is bad - you do not see all digits. If you make an operation that mathematically gives the same result it might not be the same numerically  A +B - B is not always A.

You can search for the double, but you need to take that double from that array. No operations.

Since you did not attach data,  most probably

1) two 0,0608477 are not equal, you are searching and finding the second one. Copy array data, copy data (right click!), from element, try to searh for it.

2) Value in the array is a little different.

Either put not strict comparison: abs (array - search value) < 1e-6 and search for True

Or Threshold array (needs to be non descending).

Message 3 of 17
(4,951 Views)

Another way to do this would be to convert the doubles to strings and compare strings. You could then determine how precise you want the match to be by choosing the number of digits in the conversion to string.

Message 4 of 17
(4,945 Views)

Ok Thanks to Everyone now i seem to understand the problem, i'm trying to fix it by adding the 1D Threshold Array but i can't quite comprehend the concept of the fractional index, how can i obtain the original index?

Anyway here's my subVI i'm using and let's say i want the precision to be up to 7 digits after the comma (like 0,0608477), how can i improve it?

0 Kudos
Message 5 of 17
(4,934 Views)

Can you please save your vi with some reasonable default values?

0 Kudos
Message 6 of 17
(4,914 Views)

Here's one case where i don't have any problem

0 Kudos
Message 7 of 17
(4,905 Views)
Solution
Accepted by topic author BonifacioLupo

Hi Bonifacio,

 

did you get my comment on setting default values in your controls?

Context menu -> data operations -> make current value default

or:

edit menu -> make current values default

 

i'm trying to fix it by adding the 1D Threshold Array but i can't quite comprehend the concept of the fractional index, how can i obtain the original index?

That fractional index will give you the interval of your search value between two adjacent values in your array.

Example: your array contains the values 2.0 and 3.0 and you search for 2.4. Then you get the result "0.4": your search value is after element 0 of the array, but not exactly element 0. It's 40% between element 0 and 1. To get your integer index you may round the fractional index as needed…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 17
(4,884 Views)

Hello again, I tried to attach my subVI with current values default but it seems the file is too large even if i zipped it, can you explain me also how to save back my subVI as if there were no current values set as default? Thanks that would be very helpful.

Also thanks to your explanation I was able to implement the 1D Threshold Array function (as you can see in the picture i just subtract 1E-7 to my control and then use it as a threshold and the round to ceiling value) and now it works for all my files.

Thank you again 😉

0 Kudos
Message 9 of 17
(4,804 Views)
Solution
Accepted by topic author BonifacioLupo

Hi Bonifacio,

 


@BonifacioLupo wrote:

Hello again, I tried to attach my subVI with current values default but it seems the file is too large even if i zipped it, can you explain me also how to save back my subVI as if there were no current values set as default?


Two notes:

  1. Limit the number of array elements to a reasonable amount. This will also reduce the file size of the VI for attaching it here.
  2. To remove the default data completely you can clear the array data using the context menu of the array control, then set the new (empty) data as default.

 


@BonifacioLupo wrote:

Also thanks to your explanation I was able to implement the 1D Threshold Array function (as you can see in the picture i just subtract 1E-7 to my control and then use it as a threshold and the round to ceiling value) and now it works for all my files.

You still don't use AutoCleanup, which makes it hard to understand your code…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 17
(4,795 Views)