kazimpal
kazimpal

Reputation: 300

Finding maxima in a vector using MATLAB

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?

Upvotes: 4

Views: 3398

Answers (3)

Patrick Lewis
Patrick Lewis

Reputation: 91

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.

Upvotes: -1

Amro
Amro

Reputation: 124563

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

Upvotes: 3

Smash
Smash

Reputation: 3802

Something much easier:

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

output:

b = [4,5]

Upvotes: -1

Related Questions