Find one array into another

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!

回答(2 个)

Stephen23
Stephen23 2016-2-18
编辑:Stephen23 2016-2-18
Funnily enough strfind is really good for this task:
numel(strfind(A,B))

2 个评论

thank you for a quick response! and a tricky question, is there the similar function for rows? if for example A=[1; 1; 0; 0] and b=[1;1] ?
Stephen23
Stephen23 2016-2-18
编辑:Stephen23 2016-2-18
  1. for simple column vectors transpose them to become row vectors.
  2. for matrices, you could try this FEX submission:

请先登录,再进行评论。

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

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

提问:

2016-2-18

编辑:

2016-2-18

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by