4

i'm trying to find local maxima of a vector of numbers using MATLAB. The built-in findpeaks function will work for a vector such as:

[0 1 2 3 2 1 1 2 3 2 1 0]

where the peaks (each of the 3's) only occupy one position in the vector, but if I have a vector like:

[0 1 2 3 3 2 1 1 2 3 2 1 0]

the first 'peak' occupies two positions in the vector and the findpeaks function won't pick it up.

Is there a nice way to write a maxima-finding function which will detect these sort of peaks?

Amro
  • 123,847
  • 25
  • 243
  • 454
kazimpal
  • 270
  • 3
  • 13

3 Answers3

3

You can use the REGIONALMAX function from the Image Processing Toolbox:

>> x = [0 1 2 3 3 2 1 1 2 3 2 1 0]
x =
     0     1     2     3     3     2     1     1     2     3     2     1     0

>> idx = imregionalmax(x)
idx =
     0     0     0     1     1     0     0     0     0     1     0     0     0
Amro
  • 123,847
  • 25
  • 243
  • 454
  • this is what i ended up using thanks. followed by shrinking the result with bwmorph to get one result for each peak. – kazimpal Nov 08 '11 at 18:39
-1
a = [ 0 1 2 3 3 2 1 2 3 2 1 ];

sizeA = length(a);

result = max(a);

for i=1:sizeA, 

    if a(i) == result(1)
       result(length(result) + 1) = i;
    end
end

result contains the max, followed by all the values locations that are equal to max.

joaquin
  • 82,968
  • 29
  • 138
  • 152
Patrick Lewis
  • 91
  • 2
  • 4
-1

Something much easier:

a = [1 2 4 5 5 3 2];
b = find(a == max(a(:)));

output:

b = [4,5]
Smash
  • 3,722
  • 4
  • 34
  • 54