Eghbal
Eghbal

Reputation: 3783

Detect contiguous numbers - MATLAB

I coded a program that create some bunch of binary numbers like this:

out = [0,1,1,0,1,1,1,0,0,0,1,0];

I want check existence of nine 1 digit together in above out, for example when we have this in our output:

out_2 = [0,0,0,0,1,1,1,1,1,1,1,1,1];

or

out_3 = [0,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0];

condition variable should be set to 1. We don't know exact position of start of ones in outvariable. It is random. I only want find existence of duplicate ones values in above variable (one occurrence or more).

PS.

We are searching for a general answer to find other duplicate numbers (not only 1 here and not only for binary data. this is just an example)

Upvotes: 0

Views: 101

Answers (1)

Divakar
Divakar

Reputation: 221624

You can use convolution to solve such r-contiguous detection cases.

Case #1 : To find contiguous 1s in a binary array -

check = any(conv(double(input_arr),ones(r,1))>=r)

Sample run -

input_arr =
     0     0     0     0     1     1     1     1     1     1     1     1     1
r =
     9
check =
     1

Case #2 : For detecting any number as contiguous, you could modify it a bit, like so -

check = any(conv(double(diff(input_arr)==0),ones(1,r-1))>=r-1)

Sample run -

input_arr =
     3     5     2     4     4     4     5     5     2     2
r =
     3
check =
     1

To save Stackoverflow from further duplicates, also feel free to look into related problems -

  1. Fast r-contiguous matching (based on location similarities).

  2. r-contiguous matching, MATLAB.

Upvotes: 7

Related Questions