Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: looking for an index and value in array
Date: Tue, 2 Dec 2008 06:10:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 18
Message-ID: <gh2jgb$65r$1@fred.mathworks.com>
References: <gh12am$kqi$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228198220 6331 172.30.248.38 (2 Dec 2008 06:10:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 2 Dec 2008 06:10:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:504299


"muzaffar" <muzaffarbashir@yahoo.com> wrote in message <gh12am$kqi$1@fred.mathworks.com>...
> Dear All,
> if c=[1 1 1 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1]
> how i can count no.of 1's between zeros. So i need to calculate numbers of 1's before a value zero and index of last 1 just before zero.
> thanks
> regards

  I assume from your description that in your example you want a set of counts of 1, 7, and 2 for the three groups of 1's surrounded by 0's and indices 11, 21, and 28 for the 1's which were last in each such group of 1's.  In other words, the initial eight 1's and the final six 1's are not to be counted since they are not surrounded at both ends by 0's?  Is that correct?  If so, you can compute them this way:

 t = diff([1,c,1]);
 cnt = find(t==1); cnt = cnt(1:end-1);
 ind = find(t==-1); ind = ind(2:end)-1;
 cnt = ind-cnt+1;

The counts and indices are given by arrays 'cnt' and 'ind', respectively.

Roger Stafford