Thread Subject:
Find quantity of sign changes in a vector

Subject: Find quantity of sign changes in a vector

From: mukim

Date: 1 Jul, 2012 16:06:09

Message: 1 of 5

i have a vector :
a=[1 -2 3 4 -4 -2 4]
now i want to find the number of sign changes in above vector? In this vector the sign has been changed for 4 times
thanks

Subject: Find quantity of sign changes in a vector

From: dpb

Date: 1 Jul, 2012 17:05:38

Message: 2 of 5

On 7/1/2012 11:06 AM, mukim wrote:
> i have a vector :
> a=[1 -2 3 4 -4 -2 4]
> now i want to find the number of sign changes in above vector? In this
> vector the sign has been changed for 4 times
> thanks

 > a=[1 -2 3 4 -4 -2 4];
 >> b=a<0; b=diff(b);b(b==0)=[];
 >> nsign=length(b)
nsign =
      4
 >>

--

Subject: Find quantity of sign changes in a vector

From: dpb

Date: 1 Jul, 2012 17:19:12

Message: 3 of 5

On 7/1/2012 12:05 PM, dpb wrote:
> On 7/1/2012 11:06 AM, mukim wrote:
>> i have a vector :
>> a=[1 -2 3 4 -4 -2 4]
>> now i want to find the number of sign changes in above vector? In this
>> vector the sign has been changed for 4 times
>> thanks
>
> > a=[1 -2 3 4 -4 -2 4];
> >> b=a<0; b=diff(b);b(b==0)=[];
> >> nsign=length(b)
> nsign =
> 4
> >>

Or, implemented more concisely...

nsign=sum(abs(diff(a<0)));

--

Subject: Find quantity of sign changes in a vector

From: james bejon

Date: 1 Jul, 2012 17:28:07

Message: 4 of 5

A very slight variation:

sum(diff(sign(a)) ~= 0)

Subject: Find quantity of sign changes in a vector

From: dpb

Date: 2 Jul, 2012 18:04:36

Message: 5 of 5

On 7/1/2012 12:28 PM, james bejon wrote:
> A very slight variation:
>
> sum(diff(sign(a)) ~= 0)

Or yet another w/o an explicit test...

sum(abs(diff(sign(a)./2)))

Skinning cats, and all that... :)

--

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

Contact us