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 19:45:05 +0000 (UTC)
Organization: Atlantic Inertial Systems
Lines: 23
Message-ID: <ghmhs1$frg$1@fred.mathworks.com>
References: <ghmadq$egh$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228851905 16240 172.30.248.37 (9 Dec 2008 19:45:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 9 Dec 2008 19:45:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1405925
Xref: news.mathworks.com comp.soft-sys.matlab:505929

"anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <ghmadq$egh$1@fred.mathworks.com>...
> An entry-level qestion.
> 
> I have a data file (1 x 5000000), which shows different states of my model during integration. I have four states and i have used 1, 2,3,4 to indicate each state. So the entire data file contain these 4 numbers. 
> 
> Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3, 1 to 4, 2 to 1, 2 to 3 etc. 

This takes 0.8 sec on my machine

    test = randi(4, [1, 5e6]);  % test vector
    mask = [1 2 4 8];           % map values
    % each transition now has unique value, bin it
    result = hist(diff(mask(test)), -7:7);
    
    % generate output mask to map values into 4x4 array
    temp = meshgrid(mask);
    outMask = temp-temp' + 8;
    
    result(outMask)

I didn't do anything with the diagonal because it sounds like you ignore it anyway.

~Adam