help-octave
[Top][All Lists]

## Re: Finding peaks/max in a graph

 From: John W. Eaton Subject: Re: Finding peaks/max in a graph Date: Tue, 13 Apr 2004 04:04:53 -0500

```David Bateman David dot Bateman at motorola dot com wrote:

:> a = [1 2 3 4 5 4 3 2 1 2 3 2 1]';  n = length(a);
:> peaks = find([a(2:n,1) - a(1:n-1,1) < 0; 1] & [1; a(1:n-1,1) - a(2:n,1)  <0])
peaks =
5
11

Just a small remark:

1) the method outlined by David fails if  multiple adjacent peaks
occur in vector a.  Try e.g. the following vector a with a 'double' - peak:
:> a = [1 2 5 5 2 1]';  n = length(a);
:> peaks = find( [a(2:n,1)-a(1:n-1,1) < 0;1] & [1;a(1:n-1,1)-a(2:n,1)<0] )
peaks = [](0x1)
:> PEAKSVAL= a( peaks )
PEAKSVAL = [](0x1)

--- Workaround ---
'<=' comparison operators must be used !
:> a = [1 2 5 5 2 1]';  n=length(a);
:> peaks=find([a(2:n,1)-a(1:n-1,1) <= 0;1] & [1;a(1:n-1,1)-a(2:n,1)<=0] )
peaks =
3
4
:> PEAKSVAL = a( peaks )
PEAKSVAL =
5
5

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```