Find one array into another
Show older comments
Hello everyone!
I've found similar discussions, but haven't found a solution for my case. I have an array A, for example [0 0 1 0 0 1 1 0 0 1 1 0 0 0] and want to know the number of times, when A consists array B=[ 1 1] - two consecutive ones. Not the number of ones in A in general, but a certain pattern. I can do this by making a cycle comparing element by element, but if the desired pattern is long it's not a good idea. Thank you in advance!
Answers (2)
numel(strfind(A,B))
2 Comments
goodermes
on 18 Feb 2016
- for simple column vectors transpose them to become row vectors.
- for matrices, you could try this FEX submission:
Jos (10584)
on 18 Feb 2016
for integer values you can (often) use the strfind trick
A = [1 1 0 1 0 1 1 1 0 1 0 1 0 1 1 0]
B = [1 1 0]
idx strfind(A,B)
% ans = 1 7 14
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!