Thread Subject: Number of changes in a value

Subject: Number of changes in a value

From: anoop Sivasankaran

Date: 9 Dec, 2008 17:38:02

Message: 1 of 10

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.

Please let me know how to do this

Subject: Number of changes in a value

From: Walter Roberson

Date: 9 Dec, 2008 17:58:41

Message: 2 of 10

anoop Sivasankaran wrote:
> 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.

> 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.
 
> Please let me know how to do this

graycomatrix(TheVector,'NumLevels',4,'GrayLimits',[1 4])

Note: this function might be part of the image processing toolbox

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Number of changes in a value

From: Roger Stafford

Date: 9 Dec, 2008 18:13:02

Message: 3 of 10

"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.
>
> Please let me know how to do this

sum(diff(data)~=0)

Roger Stafford

Subject: Number of changes in a value

From: Malcolm Lidierth

Date: 9 Dec, 2008 18:29:03

Message: 4 of 10

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

Subject: Number of changes in a value

From: Walter Roberson

Date: 9 Dec, 2008 18:54:33

Message: 5 of 10

Roger Stafford wrote:
> "anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <ghmadq$egh$1@fred.mathworks.com>...

>> 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.

> sum(diff(data)~=0)

Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but
certainly that isn't clear.

Interesting, your formulation was the fastest of the several I tried
at about 0.15 seconds; several variants came in about 0.16 seconds, and
sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds

The graycomatrix that I suggested in a posting above takes about 4 seconds
to produce the full 4 x 4 table.

And then there is:

[A,B] = meshgrid(1:4,1:4);
arrayfun(@(a,b) sum(foo(1:end-1)==a&foo(2:end)==b), A,B)
which takes about 6 seconds for the arrayfun

I couldn't seem to formulate the equivalent in bsxfun.

Subject: Number of changes in a value

From: Steve Amphlett

Date: 9 Dec, 2008 19:17:04

Message: 6 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <y9z%k.777$Ir1.624@newsfe05.iad>...
> Roger Stafford wrote:
> > "anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <ghmadq$egh$1@fred.mathworks.com>...
>
> >> 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.
>
> > sum(diff(data)~=0)
>
> Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but
> certainly that isn't clear.
>
> Interesting, your formulation was the fastest of the several I tried
> at about 0.15 seconds; several variants came in about 0.16 seconds, and
> sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds
>
> The graycomatrix that I suggested in a posting above takes about 4 seconds
> to produce the full 4 x 4 table.
>
> And then there is:
>
> [A,B] = meshgrid(1:4,1:4);
> arrayfun(@(a,b) sum(foo(1:end-1)==a&foo(2:end)==b), A,B)
> which takes about 6 seconds for the arrayfun
>
> I couldn't seem to formulate the equivalent in bsxfun.

This looks like a problem for sparse() to solve . Each pair of consecutive states (values) could be indices into a sparse 2D array that gets fed with ones and adds them up. Not having ML at home, I thought I'd at least share this idea so that someone could maybe finish it off.

% Excuse syntax, probably wrong, but the right idea
x=sparse(data(1:end-1),data(2:end),ones(length(data)-1),1);

... or something along those lines. Then zero the diagonal. The result would be a 4x4 sparse matrix which each (row,col) containing the number of transitions from (row) to (col).

Subject: Number of changes in a value

From: Adam

Date: 9 Dec, 2008 19:45:05

Message: 7 of 10

"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

Subject: Number of changes in a value

From: Jos

Date: 10 Dec, 2008 10:29:02

Message: 8 of 10

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <ghmg7g$ic6$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <y9z%k.777$Ir1.624@newsfe05.iad>...
> > Roger Stafford wrote:
> > > "anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <ghmadq$egh$1@fred.mathworks.com>...
> >
> > >> 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.
> >
> > > sum(diff(data)~=0)
> >
> > Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but
> > certainly that isn't clear.
> >
> > Interesting, your formulation was the fastest of the several I tried
> > at about 0.15 seconds; several variants came in about 0.16 seconds, and
> > sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds
> >
> > The graycomatrix that I suggested in a posting above takes about 4 seconds
> > to produce the full 4 x 4 table.
> >
> > And then there is:
> >
> > [A,B] = meshgrid(1:4,1:4);
> > arrayfun(@(a,b) sum(foo(1:end-1)==a&foo(2:end)==b), A,B)
> > which takes about 6 seconds for the arrayfun
> >
> > I couldn't seem to formulate the equivalent in bsxfun.
>
> This looks like a problem for sparse() to solve . Each pair of consecutive states (values) could be indices into a sparse 2D array that gets fed with ones and adds them up. Not having ML at home, I thought I'd at least share this idea so that someone could maybe finish it off.
>
> % Excuse syntax, probably wrong, but the right idea
> x=sparse(data(1:end-1),data(2:end),ones(length(data)-1),1);
>
> ... or something along those lines. Then zero the diagonal. The result would be a 4x4 sparse matrix which each (row,col) containing the number of transitions from (row) to (col).

Indeed, sparse is the way to go:

V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;
A = full(sparse(V(1:end-1),V(2:end),1))

so that A(i,j) holds the number of changes from i to j

Jos


Subject: Number of changes in a value

From: anoop Sivasankaran

Date: 10 Dec, 2008 15:44:04

Message: 9 of 10



> Indeed, sparse is the way to go:
>
> V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;
> A = full(sparse(V(1:end-1),V(2:end),1))
>
> so that A(i,j) holds the number of changes from i to j
>
> Jos
>
>
 Thank you, Sparse is probably the easyest way to do it. What are the diagonal elements?

Subject: Number of changes in a value

From: Jos

Date: 10 Dec, 2008 15:48:02

Message: 10 of 10

"anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <ghoo44$e2f$1@fred.mathworks.com>...
>
>
> > Indeed, sparse is the way to go:
> >
> > V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;
> > A = full(sparse(V(1:end-1),V(2:end),1))
> >
> > so that A(i,j) holds the number of changes from i to j
> >
> > Jos
> >
> >
> Thank you, Sparse is probably the easyest way to do it. What are the diagonal elements?

"changes" from state K to state K ...

Jos

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com