Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Counting consecutive 1s in a matrix
Date: Tue, 1 Mar 2011 18:47:21 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 49
Message-ID: <ikjevp$o2l$1@fred.mathworks.com>
References: <ikj2pa$hj1$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1299005241 24661 172.30.248.46 (1 Mar 2011 18:47:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 1 Mar 2011 18:47:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:713246

"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