Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting consecutive 1s in a matrix

Subject: Counting consecutive 1s in a matrix

From: Craig

Date: 1 Mar, 2011 15:19:06

Message: 1 of 6

This is an example of my matrix (mine is much bigger)
V =

     1 1 0
     1 1 0
     1 1 0
     1 2 0
     1 2 0
     1 3 0
     1 4 0
     2 1 0
     2 2 1
     2 3 1
     2 4 1
     2 5 1
     2 6 1
     2 6 1
     3 1 0
     3 2 1
     3 2 0
     3 3 0
     3 4 0
     3 4 0
     3 4 1
     3 4 1
     3 4 1
     3 4 1
     3 4 0
     3 4 0
     3 4 1

Can anybody give me a method to count the longest chain of consecutive 1's in the third column, but where the 1st and 2nd column in each chain are the same throughout the chain.
i.e. the longsest chain of 1's in this example is 4, where the 1st column is a 3 and the second column is a 4.

Subject: Counting consecutive 1s in a matrix

From: Bruno Luong

Date: 1 Mar, 2011 15:41:21

Message: 2 of 6

Possibly this might help:
http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter

V =[ 1 1 0
     1 1 0
     1 1 0
     1 2 0
     1 2 0
     1 3 0
     1 4 0
     2 1 0
     2 2 1
     2 3 1
     2 4 1
     2 5 1
     2 6 1
     2 6 1
     3 1 0
     3 2 1
     3 2 0
     3 3 0
     3 4 0
     3 4 0
     3 4 1
     3 4 1
     3 4 1
     3 4 1
     3 4 0
     3 4 0
     3 4 1 ]

% SplitVec from http://www.mathworks.com/matlabcentral/fileexchange/24255
[lgt row] = SplitVec(V,[],'length','firstelem');
max(lgt(row(:,3)==1))

% Bruno

Subject: Counting consecutive 1s in a matrix

From: Roger Stafford

Date: 1 Mar, 2011 18:47:21

Message: 3 of 6

"Craig " <gizmocraig@hotmail.co.uk> wrote in message <ikj2pa$hj1$1@fred.mathworks.com>...
> This is an example of my matrix (mine is much bigger)
> V =
>
> 1 1 0
> 1 1 0
> 1 1 0
> 1 2 0
> 1 2 0
> 1 3 0
> 1 4 0
> 2 1 0
> 2 2 1
> 2 3 1
> 2 4 1
> 2 5 1
> 2 6 1
> 2 6 1
> 3 1 0
> 3 2 1
> 3 2 0
> 3 3 0
> 3 4 0
> 3 4 0
> 3 4 1
> 3 4 1
> 3 4 1
> 3 4 1
> 3 4 0
> 3 4 0
> 3 4 1
>
> Can anybody give me a method to count the longest chain of consecutive 1's in the third column, but where the 1st and 2nd column in each chain are the same throughout the chain.
> i.e. the longsest chain of 1's in this example is 4, where the 1st column is a 3 and the second column is a 4.
- - - - - - - - - -
V1 = [[0 0 0];V]; V2 = [V;[0 0 0]];
t = V1(:,1)~=V2(:,1) | V1(:,2)~=V2(:,2);
s = find(V2(:,3)==1 & (V1(:,3)~=1 | t));
e = find(V1(:,3)==1 & (V2(:,3)~=1 | t));
n = max(e-s); % <-- Maximum chain length

To find the row index where the selected maximum length chain starts do

[n,ix] = max(e-s);
ix = s(ix); % <-- Row index of max. chain

in place of "n = max(e-s);"

Roger Stafford

Subject: Counting consecutive 1s in a matrix

From: Bruno Luong

Date: 1 Mar, 2011 19:49:35

Message: 4 of 6

Another way:

idx = [find(any(diff(V,1,1),2)); size(V,1)];
lgt = diff([0; idx]);
lmax = max(lgt(V(idx,3)==1));

Bruno

Subject: Counting consecutive 1s in a matrix

From: Craig

Date: 10 Mar, 2011 19:08:08

Message: 5 of 6

"Roger Stafford" wrote in message <ikjevp$o2l$1@fred.mathworks.com>...
> "Craig " <gizmocraig@hotmail.co.uk> wrote in message <ikj2pa$hj1$1@fred.mathworks.com>...
> > This is an example of my matrix (mine is much bigger)
> > V =
> >
> > 1 1 0
> > 1 1 0
> > 1 1 0
> > 1 2 0
> > 1 2 0
> > 1 3 0
> > 1 4 0
> > 2 1 0
> > 2 2 1
> > 2 3 1
> > 2 4 1
> > 2 5 1
> > 2 6 1
> > 2 6 1
> > 3 1 0
> > 3 2 1
> > 3 2 0
> > 3 3 0
> > 3 4 0
> > 3 4 0
> > 3 4 1
> > 3 4 1
> > 3 4 1
> > 3 4 1
> > 3 4 0
> > 3 4 0
> > 3 4 1
> >
> > Can anybody give me a method to count the longest chain of consecutive 1's in the third column, but where the 1st and 2nd column in each chain are the same throughout the chain.
> > i.e. the longsest chain of 1's in this example is 4, where the 1st column is a 3 and the second column is a 4.
> - - - - - - - - - -
> V1 = [[0 0 0];V]; V2 = [V;[0 0 0]];
> t = V1(:,1)~=V2(:,1) | V1(:,2)~=V2(:,2);
> s = find(V2(:,3)==1 & (V1(:,3)~=1 | t));
> e = find(V1(:,3)==1 & (V2(:,3)~=1 | t));
> n = max(e-s); % <-- Maximum chain length
>
> To find the row index where the selected maximum length chain starts do
>
> [n,ix] = max(e-s);
> ix = s(ix); % <-- Row index of max. chain
>
> in place of "n = max(e-s);"
>
> Roger Stafford


Thanks a lot that help loads, could you explain what is happening at each stage please because I need to put that in but i dont have a clue

Subject: Counting consecutive 1s in a matrix

From: Roger Stafford

Date: 10 Mar, 2011 22:09:07

Message: 6 of 6

"Craig " <gizmocraig@hotmail.co.uk> wrote in message <ilb7io$7fg$1@fred.mathworks.com>...
> "Roger Stafford" wrote in message <ikjevp$o2l$1@fred.mathworks.com>...
> > V1 = [[0 0 0];V]; V2 = [V;[0 0 0]];
> > ........
> Thanks a lot that help loads, could you explain what is happening at each stage please because I need to put that in but i dont have a clue
- - - - - - - - - - -
  Well, to begin with, by the way V1 and V2 are defined, if we compare elements in the same position of V1 and V2, we are testing for changes that have occurred. For example, if in position 5, we have V1(5,3) = 0 and V2(5,3) = 1, we know that elements in the third column of V have changed from a 0 at its position 4 to a 1 at position 5.

  The s vector is designed to point to wherever one of your "chains" has begun. By the logic in its expression, a chain is understood to begin wherever a 1 has occurred in column 3 of V {V2(:,3)==1} and either there was a 0 there in the previous position {V1(:,3)~=1} or there has been a change in either column 1 or column 2 {t = V1(:,1)~=V2(:,1) | V1(:,2)~=V2(:,2)}.

  The e vector is to point just one past the end of each chain. That is true whenever column 3 was a 1 {V1(:,3)==1} and either there is now a 0 in column 3 {V2(:,3)~=1} or else there was a change in either column 1 or 2 {t again}.

  Accordingly the pointers in s and e are paired and the differences between corresponding pairs gives the length of the corresponding chain between. This explains why n = max(e-s) gives the length of the longest chain.

  The 0 that was placed in the first position, column 3 of V1 ensures that a chain can start even at position 1 of V when there would actually be no earlier element to compare it with. The 0 in the last column 3 position of V2 ensures that a chain that extends clear to the end of V would have an 'e' pointer pointing one past the end of V because of that fictitious 0.

  That should explain my particular thinking on the problem. When you "put that in" I hope you will reword it to be more lucid than this hasty explanation of mine.

  You should realize that Bruno's solution here involved less complexity than mine and is probably faster. Certainly it involves fewer lines. It might even be easier to explain.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us