From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
02-26-2018 10:10 PM - edited 02-26-2018 10:12 PM
I am trying to locate all instances of a value displayed in an array. For example if I had an array like :
1 2 3 4 5 5 6 7 8 2 1
and I use 1D search program to find 2, I should get an array of two locations in the output. I tried using the output as an array and gave me broken wires.
please guide me if possible.
Solved! Go to Solution.
02-26-2018 10:29 PM
I think this will solve the issue.
But the thing that i am having trouble with is that i need to display -1 if the value to find is not in the array. As of now it shows 0 which could be confusing given array location starts at 0.
please help if anyone can
thank you
02-27-2018 01:15 AM - edited 02-27-2018 01:26 AM
I guess you found my old code. (please have some respect and don't chop it up like that :o. You also need to stop the loop if the search returns <0. If it returns =0, you have a valid match)
"-1" is needed if you search for one and it is not found. Here you search for all instances, so if none are found, you'll end up with an empty array, which seems to be a clear result.
The output is now an array, not a scalar. If you want an array with a single -1 element, just detect if the result is empty and switch the output after the while loop before the indicator.
02-27-2018 01:20 AM
Hi,
The search function will return -1 if there is no match found.
Increment the index returned by the search function only when there is a match and pass it to shift register.
02-27-2018 01:24 AM
You are almost done. You are wrong with the value of zero. The function gives you value "-1" if the value is not found.
So you will get a single element array with value -1 if no match. If there are matches, you get their indexes, and a closing "-1" indicating there are no more matches:
Or you can just use the OpenG Array addon function. Using this function, you only get the indexes of the found elements. If the search results in zero match, you get an empty array simply:
02-27-2018 01:39 AM
Here an other solution, you can add an incrementation for the indexes if necessary:
Best regards
02-27-2018 01:57 AM
@louis57 wrote:
Here an other solution, you can add an incrementation for the indexes if necessary:
Best regards
Either using the Search function, or the above equal? function, never forget that it is tricky to compare floating point numbers. You might think 2 = 2, but in reality they are not exactly 2. You can switch to integers instead, or using some trick like machine sigma comparison.
02-27-2018 02:14 AM
My code from 2005 is from way before conditional tunnels were available. Yes, conditional tunnels provide now another solution.
(And yes, I agree that both search array and equal/not equal comparisons on DBLs can be problematic, so be aware of that)
02-27-2018 02:48 AM
And of course it is called epsilon, not sigma 😄
http://zone.ni.com/reference/en-XX/help/371361P-01/glang/machine_epsilon/
02-27-2018 07:46 AM
@altenbach
i am really sorry, i did not mean to "steal" your code. I was reading discussion posts regarding arrays and found your post.
I tried give a thumps up (kudos) to your original post and my internet connection got interrupted somehow and i could not come online all day. Sorry about this.
also i did not mean to modify your code to make it worse, i was just trying to get -1 when the element is not in the array. Sorry again.