Count elements containing less than 3 consecutive zeros inside a vector in Matlab

For example, I have the following vector:

A = [34 35 36 0 78 79 0 0 0 80 81 82 84 85 86 102 0 0 0 103 104 105 106 0 0 107 201 0 202 203 204];

Each element inside A represents a value in every second. I want to count elements containing less than 3 consecutive zeros in =>, I will get duration values ​​in seconds for A.

In this case, the counting stops before every three consecutive zeros and starts after three consecutive zeros, etc. This is something like this:

A = [34->1s 35->2s 36->3s 0->4s 78->5s 79->6s stop 80->1s 81->2s 82->3s 84->4s 85->5s 86->6s 102->7s stop 103->1s 104->2s 105->3s 106->4s 0->5s 0->6s 107->7s 201->8s 0->9s 202->10s 203->11s 204->12s];

The result would be the following:

Duration = [6 7 12]; in seconds

Anyone have an idea?

+4
source share
5 answers

A '0' '1' () , , strsplit, .

N = 3 - . :

Duration = cellfun(@numel, strsplit(char((A>0)+'0'), repmat('0',1,N)));

, N . , A = [1 2 3 0 0 0 0 4 5] Duration = [3 3], .

N , :

Duration = cellfun(@numel, regexp(char((A>0)+'0'), [repmat('0',1,N) '+'], 'split'));

A = [1 2 3 0 0 0 0 4 5] Duration = [3 2].

+2

convolution -

%// Input
A = [0 0 0 0 0 0 0 34 35 36 0 78 79 0 0 0 ...
    80 81 82 84 85 86 102 0 0 0 103 104 105 106 0 0 107 201 0 202 203 204];

%// Mask with all >= groups of three consecutive 0 set at 0's, 1 elsewhere 
M = conv(double(conv(double(A==0),ones(1,3),'same')>=3),ones(1,3),'same')==0

%// Append with 0 to get their indices in the next step
dfd = diff([0 M 0])

%// Get indices for falling and rising edges and subtract them for Duration
Duration = find(dfd==-1) - find(dfd==1)

-

>> A
A =
  Columns 1 through 14
     0     0     0     0     0     0     0    34    35    36     0    78    79     0
  Columns 15 through 28
     0     0    80    81    82    84    85    86   102     0     0     0   103   104
  Columns 29 through 38
   105   106     0     0   107   201     0   202   203   204
>> M
M =
  Columns 1 through 14
     0     0     0     0     0     0     0     1     1     1     1     1     1     0
  Columns 15 through 28
     0     0     1     1     1     1     1     1     1     0     0     0     1     1
  Columns 29 through 38
     1     1     1     1     1     1     1     1     1     1
>> Duration
Duration =
     6     7    12
+3

conv:

Duration = diff([0,find(~conv(A, ones(1,3))),numel(A)+3])-3
+2
A = [34 35 36 0 78 79 0 0 0 80 81 82 84 85 86 102 0 0 0 103 104 105 106 0 0 107 201 0 202 203 204];
count=0;
count2=0;
Duration = [];
for i=1:(size(A,2)-3)
count2=count2+1;
if(A(1,i+1)==0)
    count=count+1;
    if(A(1,i+2)==0)
        count=count+1;
    else
        count=0;
    end
    if(A(1,i+3)==0)
        count=count+1;
    else
        count=0;
    end
    if(count==3)
        Duration = [Duration,count2 ];
        count2=-3;

         end
       else
    count=0;
     end
     end
        Duration = [Duration,count2+3 ];

    Duration

The code detects 3 consecutive zeros, the second counter keeps track of the duration. Hope this helps.

+1
source

If you go to Fileexchange and grab a seqle , you can use it to retrieve all runs of null and nonzero elements, which makes searching easier.

0
source

All Articles