Find array index inside another array

matlab find position of value in array
matlab find array in array
matlab ismember
matlab find first instance of value in array
matlab intersect
array of objects javascript
javascript array
array in array javascript

Lets say i have two numpy arrays or lists example

a = ['P', 'P' ,'Q', 'Q','P' ,'P','Q', 'Q']
b = ['Q', 'Q','P' ,'P']

and the result that i expect would be that it starts at

start = 2
end = 6

Example below without converting to string

a = ['P', 'P' ,'Q', 'Q','P' ,'P','Q', 'Q']
b = ['Q', 'Q','P' ,'P']

for i in range(len(a)):
    if a[i:i+len(b)] == b:
        print i,i+len(b)
        break


Finding the indices of the elements of one array in another , Given two vectors A and B, find the index, idx into A of the element of B so that. A(​idx)=B. Now I know there must be many ways it can be done, but is there a  FindIndex<T> (T [], Int32, Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element.


It is easiest, if you convert the lists to a string:

a1 = ''.join(a)
b1 = ''.join(b)

then you can use the str.find() function that returns the lowest index of the substring if it is found in given string. If its is not found then it returns -1.

Find indices of one array in another array., Find indices of one array in another array.. Learn more about find, index, array. How to search for an array inside another array?. Learn more about matlab, arrays, find, indexing, matrix manipulation MATLAB


thanks for you help, based on commentary discussion i was able to find this

a = ['P', 'P' ,'Q', 'Q','P' ,'P','Q', 'Q']
b = ['Q', 'Q','P' ,'P']

start = '|'.join(a).split('|'.join(b))[0].count('|')
end  = start + len(b)

if start == len(a)-1:
    print('the subset wasnt found')
else:
    print('the subset was found, start: ', start, 'end: ',end)

Data Structures: Objects and Arrays :: Eloquent JavaScript, The notation for getting at the elements inside an array also uses square brackets​. expression, with another expression inside of them, will look up the element To find out what properties an object has, you can use the Object.keys function  Binary search: Binary search can also be used to find the index of the array element in an array. But the binary search can only be used if the array is sorted. Java provides us with an inbuilt function which can be found in the Arrays library of Java which will rreturn the index if the element is present, else it returns -1.


5. Working with Arrays and Loops, Arrays in JavaScript are zero-based, which means the first element index is zero, and you might want to traverse an array until you find either a specific element, are managed by creating a new array as an element within an existing array. Thanks for the three solutions. Here is a function to test all three. "method_1" wins as the most elegant and fastest, but the other two taught other useful ways of looking at the problem.


jQuery.inArray(), Description: Search for a specified value within an array and return its index (or -​1 if not indexOf() method in that it returns -1 when it doesn't find a match. If you need the index of the found element in the array, use findIndex (). If you need to find the index of a value, use Array.prototype.indexOf (). (It’s similar to findIndex (), but checks each element for equality with the value instead of using a testing function.)


jQuery.map(), The $.map() method applies a function to each item in an array or object and arguments: The element's value and its index or key within the array or object. Let's say I have 2 arrays of double, call then A and B. If both have unique entries and I want to find the position of each element of A in array B I can do: