Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Number of changes in a value
Date: Tue, 9 Dec 2008 18:29:03 +0000 (UTC)
Organization: King's College London
Lines: 25
Message-ID: <ghmddf$20g$1@fred.mathworks.com>
References: <ghmadq$egh$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 1228847343 2064 172.30.248.38 (9 Dec 2008 18:29:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 9 Dec 2008 18:29:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1477
Xref: news.mathworks.com comp.soft-sys.matlab:505908


If you need only the total number of changes that is just
>> sum(diff(vector));

There are many ways to find specific transitions. Sticking to diff, combine with find/sort, e.g. for 3 to 4:

>> vector=[1 2 3 4 5 3 2 3 4 5 6]
vector =
     1     2     3     4     5     3     2     3     4     5     6
>> threes=find(vector==3)+1 % Note +1
threes =
     4     7     9
>> fours=find(vector==4)
fours =
     4     9
>> buffer=sort([threes fours])
buffer =
     4     4     7     9     9

To get the total:

>> sum(~diff(buffer)) % Note the ~ this time
ans =
     2

P.S. don't use "ones" as a variable name - that's an ML function