How can I search a binary array inside other bigger binary array?

For example I have the A array
A = [1 0 0 1 0,0 0 0 0 0,0 1 0 1 0,0 0 0 0 0]
I need to seach how many times is the B array in A.
B = [1 0,0 0]

댓글 수: 2

Jan
Jan 2015년 10월 26일
편집: Jan 2015년 10월 26일
Are you sure that both arrays are vectors? The commas might indicate, that you want matrices.
Would "4 times" be a satisfying answer?
No, you´re right, they are matrices. And yes, 4 times would be a satisfying answer.

댓글을 달려면 로그인하십시오.

 채택된 답변

Thorsten
Thorsten 2015년 11월 6일
편집: Thorsten 2015년 11월 6일
A = [1 0 0 1 0;0 0 0 0 0;0 1 0 1 0;0 0 0 0 0]
B = [1 0;0 0]
Embed A into a larger matrix of nans because nlfilter sets boundary values to 0; the following code is just valid for 2x2 matrices B
A2 = nan(size(A)+2))
A2(2:end-1, 2:end-1) = A;
nnz(nlfilter(A2, [2 2], @(x) (isequal(x,B))))

댓글 수: 3

Thanks. That works perfect for my needs.
I have a problem with the code. If a change matrix B, for example:
B = [0 0;0 0]
I don´t get the right number of times this pattern is in matrix A (in this case it must be find "1". Could you help me?
Thanks
Thanks Thorsten.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 10월 26일

댓글:

2015년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by