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

2 views (last 30 days)
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 Comments
Jan
Jan on 26 Oct 2015
Edited: Jan on 26 Oct 2015
Are you sure that both arrays are vectors? The commas might indicate, that you want matrices.
Would "4 times" be a satisfying answer?
AMANECH77
AMANECH77 on 6 Nov 2015
No, you´re right, they are matrices. And yes, 4 times would be a satisfying answer.

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 6 Nov 2015
Edited: Thorsten on 6 Nov 2015
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 Comments
AMANECH77
AMANECH77 on 6 Nov 2015
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!