Code covered by the BSD License  

Highlights from
Local min, max, nearest neighbour

4.53571

4.5 | 28 ratings Rate this file 57 Downloads (last 30 days) File Size: 6.64 KB File ID: #3170

Local min, max, nearest neighbour

by Sergei Koptenko

 

21 Mar 2003 (Updated 13 Apr 2004)

Find local minima, maxima, match value in vector.

| Watch this File

File Information
Description

Set of functions to look for local minima or maxima in a vector, find a point inside a vector closest to a certain value and small function to clear everything.

Acknowledgements
This submission has inspired the following:
Plot cylinder
MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (36)
26 Mar 2003 Sergei Koptenko

Appology to all who loaded this file!
1) one bug was found and fixed in lmax_pw.m and lmin_pw.m
2) match.m wasn't stable dealing when first and last points were in the range- Fixed
Please reload zipfile in two-three days (new name should be "local_ext_mod.zip")

29 Jul 2003 Nick Disabato

I'm using this to find minima in a plot which involves a lot of sharp downward spikes; the goal is to find at what points these spikes land. It's a great program, but a future improvement may be to add in code that will register a minima if it attains a certain percent difference from a group of points previous to it. This seems sort of eccentric, but it may help in applied mathematical applications

11 Feb 2005 David Liu

I guess there is a bug when the "peak" is flat, such as a=[3 4 5 5 4 3]. lmax returns empty.

15 Feb 2005 Sergei Koptenko

Not quite a bug but a feature :-)
If you go into the function body there are four lines commented out NN 41, 42, 45 and 46 that define if flat spots should be included in results. If you un-comment them, function will give a correct answer.

16 Feb 2005 Bing Ouyang

Here is another example that it did not report correct answer.

If the input has two flat steps:
       a=[0 1 1 2 2 0]
The middle step will be considered a local maxima...
indd =[2 4]

21 Feb 2005 Sergei Koptenko

1) another example? I?m sorry Bing, but if you just try to implement measures I mentioned in the first message you will see that function indeed gives the correct answer to the first example.
2) Concerning your example, the correctness of the answer lay not in the function?s output but in your original assumptions. The problem here is how to define the local maxima for a long flat spot. For instance, in [0 0 1 1 0 0] position of maximum can be defined at 3rd OR at the 4th position, at your liberty. In vector [0 0 1 1 2 2 1], 0011 can be defined as one-sided local maximum (a ledge) or as an absence of the maximum, depending on your views and needs. This is why I chose to adhere to the strict maxima criteria excluding flat spots altogether, but giving the possibility of including them if such a need arises through commented lines in the code.

29 Mar 2005 Dragos Maciuca  
08 Dec 2005 Simon de Leon

minor bug:
returns empty matrices for
[a,b]=localmax([0 0 0 0 2 0])

fix:
in max/min search loop, change
--> while i < len_x-1
to
-->while i < len_x

Other than that, does exactly as described.

12 Dec 2005 Daniel C

it's very good

11 Jan 2006 Leon King

great. works like a charm

19 Feb 2006 Felix TTTTTTTTT

[a,b]=localmax([0 2 0 0 0 0]) also has to tuned by hand. Nie code, but some rough spots. Could be improved.

22 Feb 2006 Rajesh Krishnan

Incredibly useful!!! lmax identifies some local minima points if the number of passes are low.

03 Apr 2006 Withawat W.

Thanks a lot! You save my time :)

23 Jun 2006 Kianoush Nazarpour

Thank you! You make the life easier!

26 Jun 2006 vamsee somavarapu

nice work and very helpful

12 Jul 2006 Sergei Koptenko

Steven Kay (skay@orbfactory.com) is a fictional character:
....If you do so, please include this problem report. You can delete your own text from the message returned below.

The Postfix program

<skay@orbfactory.com>: host cast.navnet.net[64.5.195.2] said: 550
    <skay@orbfactory.com>: User unknown in local recipient table (in reply to
    RCPT TO command)....

19 Aug 2006 Niranjan Prasad

The life became easier now.

31 Aug 2006 jon erickson

Works great...a real time saver.
Another user commented a while back that he is using it for "spike" detection. I am doing the same. If you want to find the spikes only, and not all local minima, first use "find" command to locate all points that have amplitudes above a certain criteria--all points with amplitude 5 sigma above the rms value of the signal, for instance

15 Sep 2006 Cynthia G.

i find extreme.m more simple to use

24 May 2007 leila ayoubian  
21 Dec 2007 Hyeongyong Jeon

Very good! thanks for sharing us~!

02 Jan 2008 L Lu  
31 Jan 2008 J D

I love you! This has made my life so much easier

05 Mar 2008 fugazi p

Interesting. How can this help me tackle my problem, here: http://www.mathworks.fr/matlabcentral/newsreader/view_thread/165101

Comments and help appreciated.

13 Mar 2008 B B

This is very helpfull! Thx! A LOT!

12 Apr 2008 o l  
15 Apr 2008 Ron Sostaric

I used the "match" function. Exactly what I was looking for, and works very well.

28 May 2008 Claudio P.

A couple of comments:
1) help for lmin is wrong, should be [b,a]=lmin... rather than [a,b]=lmin...
2) lmax is not finding the exact max with high 'filt' values, I had to increase the rng value at lmax.m line 55 in oder to have it wokring fine (maybe rng should not have a fixed value but should be proportional to filt?)

apart from that, everything ok ;)

02 Jun 2008 R Jermia

Excellent!! It rocks.

22 Aug 2008 Sean Vitousek

Nice! Works quite well.

25 Aug 2008 Robert Hovden

Great, simple program. Works well, good examples. This program will save you time since there is no need for you to write what Koptenko has already done well.

12 Sep 2008 levent yüksek

Usefull program saved my time. Thanx

18 Feb 2009 Peter Lo  
18 Feb 2009 Peter Lo

Nice work. It save a lot of my time.
Two comments:
1) Would like to see you add a threshold value so that it only find min and max that are below or above the threshold. I made a simple modification to the lmax and lmin to include that
2) Would also like to see adding the filtered curve to plot with the graph. I see the same problem with Claudio P. that in a high noise situation, the maxima are different for low and values of filt. Adding the smoothed curve will help me to determine which filt value to use

01 Apr 2009 Idillus

Nice ans usefull

24 Nov 2011 Q L

Thank you so much.

Please login to add a comment or rating.
Updates
27 Mar 2003

Bug fix in match.m and lmax_pw.m

27 Mar 2003

Bug Fix

13 Apr 2004

Bug Fix

Tag Activity for this File
Tag Applied By Date/Time
peak detection Sergei Koptenko 22 Oct 2008 06:58:38
local min Sergei Koptenko 22 Oct 2008 06:58:38
max Sergei Koptenko 22 Oct 2008 06:58:38
nearest neighbour Sergei Koptenko 22 Oct 2008 06:58:38
local min Moataz 14 Dec 2009 09:17:49
nearest neighbour Will Michalak 14 Mar 2010 13:41:53
max Bruno Carvalho 21 Jan 2011 05:12:13
local min Feng 26 May 2011 22:07:06
local min Anthi 12 Aug 2011 10:43:04
local min Tiffany 24 Aug 2011 20:50:20
local min Asif 07 Sep 2011 15:50:23
nearest neighbour Asif 07 Sep 2011 15:50:40
max Asif 07 Sep 2011 15:50:42
peak detection Asif 07 Sep 2011 15:50:45
nearest neighbour Bill Smith 12 Oct 2011 08:04:00
local min jean-michel roger 02 Jan 2012 10:21:30
local min Pavel 27 Apr 2012 10:16:13
local min LC Smith 15 May 2012 06:34:18
local min reza 28 May 2012 02:43:24

Contact us at files@mathworks.com