No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
local maxima \ minima

4.4 | 22 ratings Rate this file 58 Downloads (last 30 days) File Size: 2.64 KB File ID: #14498 Version: 1.0
image thumbnail

local maxima \ minima


Yonathan Nativ (view profile)


01 Apr 2007 (Updated )

a simple (but effective) code to find local maximas

| Watch this File

File Information

This is a very simple function to find the local maximum in any dimensional array. As simple as it is it still gives nice results.

I use the imdilate() function as a maximum operation and then compare the data to the result.

The function receives three parameters:
the data, a vector defining the minimum distance between peaks in each of the data dimensions. and a flag either to exclude equal points or not.

use examples:
a = cumsum(randn(1000,1));
peaks = localMaximum(a,[100]);
figure; plot(a); hold on; plot(peaks,a(peaks),'ro');

[x y] = meshgrid(-6:0.1:6,-6:0.1:6);
a = sinc(x).*sinc(y);
lMaxInd = localmaximum(a,[20 20]);
lMinInd = localMaximum(-a,[20 20]);
figure; mesh(x,y,a); hold on;

- It is recommended to run (if possible) a LPF on the data before searching for the peaks

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
10 Jul 2016 wang zg


07 Jul 2016 BIG

BIG (view profile)

07 Jul 2016 BIG

BIG (view profile)

very good

27 Mar 2016 houzhixin

23 Nov 2015 Panfeng Ding

17 Jul 2015 wang nannan

19 Nov 2014 junwei

junwei (view profile)

Thank you very much´╝üThe function performs well.

20 Oct 2014 Dan

Dan (view profile)

if the input data vector is a double vector, the following line will run error
X = imdilate(x,se);

how do i modify this function to the double vector case?

Comment only
10 May 2014 Fred

Fred (view profile)

19 Jul 2013 Jawahir

05 Mar 2013 gu

gu (view profile)

01 Feb 2013 Qiao Liyong

Thank you very much. this code is very helpful in find maxima in the matrices.

12 Sep 2012 michael scheinfeild

very nice implementation of gray scale dilation to find max

21 May 2012 ayialy

ayialy (view profile)

13 Dec 2011 shin

shin (view profile)

21 Jul 2011 Andreas

11 Nov 2009 sophie neve

good code :)

20 Mar 2009 Thomas Clark


Nice, clean implementation. I'm a real fan of how concise this is - doing a similar thing without an image dilation makes for some really clunky code.

Shame that it includes the dependency on the image processing toolbox; I haven't found any freeware routines which do image dilation and could be used as a swap-out for imdilate. I'll let you know if I come across any.

The documentation could be a bit clearer (an explanation of what's going on fundamentally would be good, as someone who doesn't know about image dilation would probably avoid it); but it's plenty to get someone up and running the code.

Good code, well worth a download.



03 Mar 2009 Jean-Luc Dellis

Jean-Luc Dellis (view profile)

Good job, thanks you !
Without saying details, i use your code to get the second minima in experimentaly acquired data. That saves me time. I add that the function is very simple to use.
Cheers !

24 Sep 2008 Daniel Schleicher

Great code!
I prefered to exclude zerovalues in the input too!
first saving the original x
% exculdeEqualPoints
x = x + rand(size(x))*minimumDiff;
se = ones(minDist);
X = imdilate(x,se);
f = find(x == X & xold~=0);

16 Aug 2007 Jarrod Petersavage

Good code, except it can catch all points on a rising edge (positive slope). If you fliplr the vector, run the algorithm in both directions and only accept points that fall in both solution bins, you get a much cleaner result. I'll try to post the code.

16 Apr 2007 Ken Garrard


Thanks for updating your submission. It is indeed an interesting and effective use of the imdilate function.

05 Apr 2007 Yonathan Nativ

Dear Ken,
Well I sew so many methods posted for local maximum all are very complicated and usually give worst results then this simple method (numeric derivation tends to be noisy).

As the guy in the newsgroup was very pleased from this simple solution I thought it might interest others.

Error checking isn't exactly algorithm worth uploading (each one can do his won..)

And finally: IMHO nothing is more important then a catchy heading !

Still thanks for the comment :)

Comment only
05 Apr 2007 Ken Garrard

The author posted this code in response to a question on the matlab newsgroup a few days ago. Basic error checking and meaningful help text are preferred over a "catchy heading."

02 Apr 2007 Dimitri Shvorob

Two lines, and two filename clashes! 100%! ;)

Comment only
16 Apr 2007

reasons for changes: nasty comments I got :)

I made the function a bit more user friendly. Instead of passing a "structure element" you pass to the function a number defining the minimum distance between two peaks.

18 Apr 2007

small improvements in the validity checks

25 May 2007

I added support of subscript and index according to the nargout

20 Sep 2007

I added an option to exclude plateau points - I do it by adding noise which won't affect the real peaks position. As it is rather heavy you might not want to use this (default option is off).

Contact us