File Exchange

## local maxima \ minima

version 1.0.0.0 (1.05 KB) by
a simple (but effective) code to find local maximas

Updated 20 Sep 2007

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 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;
plot3(x(lMaxInd),y(lMaxInd),a(lMaxInd),'k*','markersize',10,'linewidth',1.5);
plot3(x(lMinInd),y(lMinInd),a(lMinInd),'g*','markersize',10','linewidth',1.5);
legend('sinc(x)sinc(y)','peaks','valleys','location','best')

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

### Cite As

Yonathan Nativ (2021). local maxima \ minima (https://www.mathworks.com/matlabcentral/fileexchange/14498-local-maxima-minima), MATLAB Central File Exchange. Retrieved .

Mebius

thanks

rongme2

zhu zhu

thank you!

wang zg

good!

BIG

BIG

very good

houzhixin

Panfeng Ding

wang nannan

junwei

Thank you very much！The function performs well.

Dan

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?

Fred

Jawahir

gu

Qiao Liyong

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

michael scheinfeild

very nice implementation of gray scale dilation to find max

ayialy

shin

Andreas

sophie neve

good code :)

Thomas Clark

Yonathan.

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.

Cheers!

Tom

Jean-Luc Dellis

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 !

Daniel Schleicher

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

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.

Ken Garrard

Yonathan,

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

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

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

Dimitri Shvorob

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

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