LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Compare an array to a value and return index

Hi, I want to find out how many elements in an array a[i][j] with value b and return their index, in c, it looks like this:
for(i=0;i<639;i++)
    for(j=0;j<479;j++)
       {
          if (a[i][j] = b)
             return i,j;
       }
by the way, the index returned should be store in another array for future process ^_^
Best thanks to anybody willing to help.
0 Kudos
Message 1 of 14
(4,072 Views)
Here's one way, purists may have a more efficient method, but I'm about to go to bed so I did a quicky:

Message Edited by Bill@NGC on 09-16-2007 10:13 PM

0 Kudos
Message 2 of 14
(4,053 Views)
(Bill, don't forget that we start counting with zero, so the iteration counts for the FOR loops shoud be 480 and 640. ;))
 
In any case, an alternative solution would be to reshape the 2D array to a 1D array, use "seach 1D array", then calculate the 2D indices later.
 
Here's an example (LV 8.0).
 
(If you expect that many elements match, it might help to preallocate the 1D index array and trim it later. (Not implemented here). You also need to decide if you want the output to be (row, column) or (column, row)). 🙂
 
 

 

Message Edited by altenbach on 09-16-2007 11:40 PM

Download All
0 Kudos
Message 3 of 14
(4,037 Views)
Thanks, Bill and alterbach,  both  solutions are  quite  good. In my case, this is an array of image data, so I need to find a particular pixel value in the array for doing some classification. The next step for me is to choose the region for processing. That is I can choose a subset of the array and find a value in it.
Thanks again for your generous help. Cheers! ^_^
0 Kudos
Message 4 of 14
(4,026 Views)
To alterbach:
aren't we supposed to sort the 1D array before search?
0 Kudos
Message 5 of 14
(4,025 Views)


@chenli868 wrote:
To alterbach:
aren't we supposed to sort the 1D array before search?

NO! Sorting would mess up all element positions and it would be impossible to reconstruct the original indices.

What do you have in mind?

(There is one little oversight. Of course the 2D array and the element to be found should be in the same representation. You need to modify both to whatever representation your data is.)

Message Edited by altenbach on 09-17-2007 12:32 AM

0 Kudos
Message 6 of 14
(4,018 Views)
sorry, miss the "not" when reading instruction about "search 1D array"Robot Sad
Thanks for the help. alterbachSmiley Very Happy
0 Kudos
Message 7 of 14
(4,009 Views)
Sorry to trouble you guys again, if I want to choose a particular area for processing, what shall I do?
I try to use array subset, but it doesn't work. Because the return indice will be the indice of the new array, not the original one.
And apologize for spell altenbach wrongSmiley Happy
0 Kudos
Message 8 of 14
(3,979 Views)
If you use array subset, then you know how index (0,0) relates to your original array, because you used those indices to do the subset.  So, add those indices back into those in the subset.  i.e., if you started your array subset at (3,4), then index (1,2) in the subarray is index (4,6) in the original array.


0 Kudos
Message 9 of 14
(3,975 Views)
Thanks Matthew. Yes, that's right. But I don't want to modify the size of the array. Is there any way that I can just put 0 to the elements that I don't want in the array?
0 Kudos
Message 10 of 14
(3,974 Views)