08-03-2017 04:17 AM
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
Solved! Go to Solution.
08-03-2017 04:36 AM
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.
08-03-2017 04:43 AM
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.
08-03-2017 04:51 AM
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"
08-03-2017 04:51 AM
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.
08-03-2017 04:58 AM - edited 08-03-2017 05:07 AM
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.
08-03-2017 05:03 AM
IanSh's/crossrulez solution would be sufficient for you, since you have to go through all elements anyways.
08-03-2017 05:37 AM
08-03-2017 05:53 AM
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.
08-03-2017 05:55 AM - edited 08-03-2017 05:55 AM
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