LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2D array search

Solved!
Go to solution

Hi all, I am trying to search through a 2D array of data and return all the indexes in an array that contain this element. I tried to use the code that is provided here: http://forums.ni.com/t5/Example-Programs/Search-2D-Array/ta-p/3513795 with some small changes. I found that the while loop inside the for loop will stop my search when there is no element with the same value as the one I am looking for in a column of a certain row of my array. So I replaced it with a for loop to check for all the elements of my array. The thing is that when I run it I see that the incoming from my array may be the same as the element I provide but the index I get is still -1. So the Search 1D array doesn't return the index of the row back. Could someone spot my mistake here and help me with this. I tried different approaches but none seem to work well. I attached here a snippet of my code. Thanks

0 Kudos
Message 1 of 19
(8,690 Views)

Since you are using a double FOR loop, all you need is an Equals to compare the current element with the search value.

 

And if you are using 2013 or newer, you can use Conditional Tunnels for the outputs.  Just right-click on the output tunnel and you will see a mode menu.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 19
(8,677 Views)

if you want you can use the "Search Array__ogtk.vi" from the OpenG tools,

it returns all occurrences for a 1D array.

which you then would have to wrap in a loop to do that for each row.

also you can have a look at their code.

zort02.png


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 3 of 19
(8,668 Views)

also, your code has several visual shortcomings (couldn't test it, don't have 2017)

 

the input of the "Data" rows in the inner for-loop should not be indexed,

this defeats the purpose of using "Seach 1D Array",

the obvous thing would have been that you build an array from a scalar to input it into "Search 1D Array".

What you want is to store (in a shift register) your last found index, increment it by 1 and try to find the next with "Search 1D Array"


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 4 of 19
(8,660 Views)
Solution
Accepted by topic author stuleo

This solution is what Crossrulz is talking about I think (you'll need the concatenating tunnel as well). The old openG array search predates conditional terminals and only searches 1D arrays.

2d array search.PNG

Ian
LabVIEW since 2012
Message 5 of 19
(8,658 Views)

Hi thank you so much for replying and going through my code.

I see what you mean but if I don't index then it will not start looking for the elements. I did this with the array builder outside the second for loop and it works but the 1D search array is not very good for taking all the indexes of the elements it only takes the first one.  I think maybe I will take completely out the 1D array search and look individually for each element. Then from my i indicators of each for loop I could get the indexes of my elements in the array.. I will try this and get back to the post.

 

EDIT: It worked this way although the indexes go in the new array not in the order they were found. I attach here the updated code.

0 Kudos
Message 6 of 19
(8,654 Views)

IanSh's/crossrulez solution would be sufficient for you, since you have to go through all elements anyways.


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 7 of 19
(8,645 Views)

zort03.png


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
Message 8 of 19
(8,629 Views)

Thanks! I am just discoverying all the nice features of LabVIEW and they are many! I wasn't aware of what concatenating was doing, now it makes sense and it actually looks a lot nicer. I think I did the same in my case by including the if structure to take these in an order. Now, my question goes a little further but I wouldn't like to open another post since we are all on the same page here. If you think I should, please let me know. I want to make a subarray of my elements whose indices are included inside the boarders of the indices I just extracted. I will try some things of my own but I would appreciate any suggestions.

0 Kudos
Message 9 of 19
(8,617 Views)

That one you just marked solution doesn't give legitimate outputs. The array of indices is misleading.

 

This suggests elements at locations (1,0)(0,3)(1,4) which is wrong. Indices 1D is great for a 1D search, but 2D was required so isn't usable here.

 

The diagram I posted further up will return an array of correct indices. If this is actually what solved it for you, please mark that one as the solution for future viewers

 

Ian
LabVIEW since 2012
0 Kudos
Message 10 of 19
(8,615 Views)