05-01-2017 11:00 AM
What is an optimal way to find the index of matching array in 1D array?
Solved! Go to Solution.
05-01-2017 11:08 AM
Hi there,
Are you simply looking for an element in the array? You can use "Search 1D Array". If you are looking for the nearest element, take the magnitude of the difference and find the smallest element.
If you are looking for an array with an array, like you have 1,2,3,4,5,6 and are looking for 2,3,4, then I think you would have to match each one in order. Or convert both to strings and search the string.
05-01-2017 11:10 AM
You will have to tell us much more so that we can discover what "optimal" really means.
What type of data, and the nature of the matching array that you will have to search for, data types, how fast...
A basic suggestion is to search for the first element of your "target array" and only when you find a match on the first, proceed to check the residual data for a match.
Ben
05-01-2017 11:37 AM
... and once you find a match, you might want to keep looking to see if there is more than one. 😄
05-01-2017 12:02 PM - edited 05-01-2017 12:15 PM
This approach is not "optimal", but it is effective.
This works even when the sub array is not an exact match to a portion of the big array.
This can find a 5,000-element subset of a 100,000-element array in 8 seconds, which is not super-fast, but may be suitable for your needs.
05-01-2017 12:19 PM
@Gregory wrote:
Or convert both to strings and search the string.
If, the elements are multi-byte, you would then need to reject matches that start at positions that are not integer multiples of the size. Still, should narrow things down quite a bit. 🙂
05-01-2017 12:46 PM
Below I attach picture with example what need to realize
05-01-2017 12:47 PM
I believe there should be a way to do this with Cross Correlation, from the 'signal operations' palette. Cross correlation is a measure of the overlappedness of two signals. It tells you how much you need to shift one signal with respect to the other to get a good overlap. Or rather, it tells you how good your overlap is with each possible amount of shift.
However, I'm finding it doesn't deal well with arrays of unequal size. So even if I get it to work, I think it'll look more complicated than my previous solution.
05-01-2017 12:58 PM - edited 05-01-2017 01:08 PM
@Valentin87 wrote:
Below I attach picture with example what need to realize
Flatten both to string and search string.
05-01-2017 12:58 PM
This method will work with Booleans, provided you know that there will be an exact match.
If you think the match is likely to be only approximate (ie, 5 out of 6), or might not happen at all, we should take some extra steps to account for that.