[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: detecting a sign change in a vector
From: |
Bob Walton |
Subject: |
Re: detecting a sign change in a vector |
Date: |
Fri, 1 Feb 2013 10:44:44 -0800 (PST) |
dirac wrote
> ...
> I have a vector, V, which basically contains the values of a cos/sin
> function. The signs of the elements in the vector change from + to -
> occasionally. I would like to find these points without using a loop as I
> have done in the past. Is there a way to do this?
> ...
> MA
Here is some code that gives the subscripts of the sign changes:
t=linspace(0,20,1001)';
x=sin(t.^2);
z=x>=0;
c=conv(z,[-1 1]);
st=find(c<-.5); %subscript of the start of a run of non-negatives
en=find(c>.5)-1; %subscript of the end of a run of non-negatives
runs=[st en]; %start and end of runs of non-negatives
Result:
runs =
1 178
252 307
356 397
436 469
503 532
562 588
615 640
665 687
710 731
753 773
794 813
833 851
870 887
905 921
939 955
972 987
--
Bob Walton
--
View this message in context:
http://octave.1599824.n4.nabble.com/detecting-a-sign-change-in-a-vector-tp4649327p4649359.html
Sent from the Octave - General mailing list archive at Nabble.com.