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!

Respuestas (2)

Stephen23
Stephen23 el 18 de Feb. de 2016
Editada: Stephen23 el 18 de Feb. de 2016

0 votos

Funnily enough strfind is really good for this task:
numel(strfind(A,B))

2 comentarios

goodermes
goodermes el 18 de Feb. de 2016
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 el 18 de Feb. de 2016
Editada: Stephen23 el 18 de Feb. de 2016
  1. for simple column vectors transpose them to become row vectors.
  2. for matrices, you could try this FEX submission:

Iniciar sesión para comentar.

Jos (10584)
Jos (10584) el 18 de Feb. de 2016

0 votos

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

Categorías

Etiquetas

Preguntada:

el 18 de Feb. de 2016

Editada:

el 18 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by