Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Find quantity of number changes in a vector

Subject: Find quantity of number changes in a vector

From: quintessod@googlemail.com

Date: 24 Mar, 2009 19:48:08

Message: 1 of 10

Hi,
Apologies if this is a newb question. I have a vector containing
numbers: 0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0
I want to count the number changes, eg above would be 4 because 0 to 1
to 0 to -1 then forthly back to 0. I could achieve using a for loop,
but would prefer a less computationally intensive solution.

Many thanks.

Subject: Find quantity of number changes in a vector

From: us

Date: 24 Mar, 2009 19:53:01

Message: 2 of 10

quintessod@googlemail.com
> I have a vector containing
> numbers: 0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0
> I want to count the number changes, eg above would be 4...

one of the solutions

     v=[0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0];
     r=sum(abs(diff(v)))
% r = 4

us

Subject: Find quantity of number changes in a vector

From: Roger Stafford

Date: 24 Mar, 2009 19:55:02

Message: 3 of 10

quintessod@googlemail.com wrote in message <28f55bdc-43ce-4c76-a1eb-ff9d4a5483f4@z15g2000yqm.googlegroups.com>...
> Hi,
> Apologies if this is a newb question. I have a vector containing
> numbers: 0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0
> I want to count the number changes, eg above would be 4 because 0 to 1
> to 0 to -1 then forthly back to 0. I could achieve using a for loop,
> but would prefer a less computationally intensive solution.
>
> Many thanks.

  If v is your vector, do this:

 n = nnz(diff(v)~=0);

Roger Stafford

Subject: Find quantity of number changes in a vector

From: Roger Stafford

Date: 24 Mar, 2009 20:55:03

Message: 4 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqbdqm$o1q$1@fred.mathworks.com>...
> If v is your vector, do this:
> n = nnz(diff(v)~=0);
> .....

  I could have done the simpler:

 n = nnz(diff(v));

  Also the usual warning: Even if two successive floating point values differ by as little as a mere rounding difference, it will count as a number change in this code.

Roger Stafford

Subject: Find quantity of number changes in a vector

From: quintessod@googlemail.com

Date: 24 Mar, 2009 23:52:09

Message: 5 of 10

On 24 Mar, 20:55, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Roger Stafford" <ellieandrogerxy...@mindspring.com.invalid> wrote in mes=
sage <gqbdqm$o1...@fred.mathworks.com>...
> > =A0 If v is your vector, do this:
> > =A0n =3D nnz(diff(v)~=3D0);
> > .....
>
> =A0 I could have done the simpler:
>
> =A0n =3D nnz(diff(v));

Many thanks Roger and US. Diff is my friend.

Subject: Find quantity of number changes in a vector

From: andywocky@gmail.com

Date: 24 Mar, 2009 23:55:09

Message: 6 of 10

On Mar 24, 3:48=A0pm, quintes...@googlemail.com wrote:
> Hi,
> Apologies if this is a newb question. =A0I have a vector containing
> numbers: 0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0
> I want to count the number changes, eg above would be 4 because 0 to 1
> to 0 to -1 then forthly back to 0. =A0I could achieve using a for loop,
> but would prefer a less computationally intensive solution.
>
> Many thanks.

... looks like the solution from "us" is faster. Median execution
time on my computer for five different runs are:

   sum(abs(diff(v))) ~ 2.72 microseconds
   nnz(diff(v)) ~ 6.00 microseconds
   nnz(diff(v)~=3D0) ~ 8.20 microseconds

Subject: Find quantity of number changes in a vector

From: Roger Stafford

Date: 25 Mar, 2009 01:58:02

Message: 7 of 10

andywocky@gmail.com wrote in message <36327e39-a39a-48f2-8eb2-8f65fa17069d@f1g2000prb.googlegroups.com>...
> ... looks like the solution from "us" is faster. Median execution
> time on my computer for five different runs are:
>
> sum(abs(diff(v))) ~ 2.72 microseconds
> nnz(diff(v)) ~ 6.00 microseconds
> nnz(diff(v)~=3D0) ~ 8.20 microseconds

  Urs wins again! Ironically he's the one from whom I first learned of 'nnz'!

Roger Stafford

Subject: Find quantity of number changes in a vector

From: Matt Fig

Date: 25 Mar, 2009 02:50:02

Message: 8 of 10

It seems to me that the sum solution could give false results depending on what exactly the vector to be examined looked like. We are only given one example, which has elements on
{-1,0,1}
Obviously if there are other elements (4 or 120 for example), the nnz solution will find the number of changes while the sum solution would fail. However, the arrangement is crucial even for the limited set. Take:

v = [ -1 0 0 0 0 1 -1]
n = nnz(diff(v))
r = sum(abs(diff(v)))




7KxK-p195K951R<3;4;-K.@K-%;:-/AA@kY;-R1;;/EK4B8.-<K8@-:4e1K

Subject: Find quantity of number changes in a vector

From: us

Date: 25 Mar, 2009 05:59:03

Message: 9 of 10

"Matt Fig"
> It seems to me that the sum solution could give false results depending on what exactly the vector to be examined looked like. We are only given one example, which has elements on
> {-1,0,1}
> Obviously if there are other elements (4 or 120 for example), the nnz solution will find the number of changes while the sum solution would fail. However, the arrangement is crucial even for the limited set...

matt, absolutely correct...
the solution was proposed on the assumption that {-1|1} blocks are split by {0}s; otherwise, roger's NNZ is the way to go, of course...
best
urs

Subject: Find quantity of number changes in a vector

From: Matt Fig

Date: 25 Mar, 2009 06:07:11

Message: 10 of 10

"us " <us@neurol.unizh.ch> wrote in message <gqch76$5uj$1@fred.mathworks.com>...

> the solution was proposed on the assumption that {-1|1} blocks are split by {0}s;

Ah, you saw a pattern that I missed, obviously I was only looking at the values in the array. Now it all makes sense!

Tags for this Thread

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.

Contact us