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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

finding array from an array

Solved!
Go to solution

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.

Download All
0 Kudos
Message 1 of 12
(3,230 Views)

Capture.PNG

 

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

0 Kudos
Message 2 of 12
(3,214 Views)
Solution
Accepted by topic author Alyssa001

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.

Message 3 of 12
(3,184 Views)

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.

 

bp
0 Kudos
Message 4 of 12
(3,180 Views)
Solution
Accepted by topic author Alyssa001

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:

 

indexes2.png

 

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:

 

indexeeee.png

 

 

0 Kudos
Message 5 of 12
(3,174 Views)
Solution
Accepted by topic author Alyssa001

Here an other solution, you can add an incrementation for the indexes if necessary:

array search.png

Best regards

0 Kudos
Message 6 of 12
(3,167 Views)
Solution
Accepted by topic author Alyssa001

@louis57 wrote:

Here an other solution, you can add an incrementation for the indexes if necessary:

array search.png

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.

0 Kudos
Message 7 of 12
(3,159 Views)

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)

0 Kudos
Message 8 of 12
(3,156 Views)
0 Kudos
Message 9 of 12
(3,148 Views)

@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.

 

 

 

0 Kudos
Message 10 of 12
(3,109 Views)