File Exchange

image thumbnail

Local min, max, nearest neighbour

version (6.64 KB) by Sergei Koptenko
Find local minima, maxima, match value in vector.


Updated 13 Apr 2004

View License

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.

Cite As

Sergei Koptenko (2021). Local min, max, nearest neighbour (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (38)

Michael Heistand

Well done! This definitely saved me a lot of time!



Thank you so much.


Nice ans usefull

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

Peter Lo

levent yüksek

Usefull program saved my time. Thanx

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.

Sean Vitousek

Nice! Works quite well.

R Jermia

Excellent!! It rocks.

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 ;)

Ron Sostaric

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

o l


This is very helpfull! Thx! A LOT!

fugazi p

Interesting. How can this help me tackle my problem, here:

Comments and help appreciated.


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

L Lu

Hyeongyong Jeon

Very good! thanks for sharing us~!

leila ayoubian

Cynthia G.

i find extreme.m more simple to use

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

Niranjan Prasad

The life became easier now.

Sergei Koptenko

Steven Kay ( 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

<>: host[] said: 550
<>: User unknown in local recipient table (in reply to
RCPT TO command)....

vamsee somavarapu

nice work and very helpful

Kianoush Nazarpour

Thank you! You make the life easier!

Withawat W.

Thanks a lot! You save my time :)

Rajesh Krishnan

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


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

Leon King

great. works like a charm

Daniel C

it's very good

Simon de Leon

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

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

Other than that, does exactly as described.

Dragos Maciuca

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.

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]

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.

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.

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

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 "")

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Plot cylinder

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!