0

In Matlab, by the function max (), I can only get one single maximum element of a vector, even if there can be several local maximum elements. I was wondering how to get values of all local maximas in MATLAB.

Peter
  • 161
  • 1
  • 11
  • finding local maxima and using the function max are completelly different things. Max gives you the maximum values, not the maxima. – Ander Biguri Aug 03 '17 at 13:20
  • Ander Biguri this is what i want to ask that how to find local maxima because I only know about the mentioned command and it gives us the 1 maximum value – Peter Aug 03 '17 at 13:22
  • But that is an optimization algorithms job. There is no "a way", depedns in your problem. Do you have an equation? just an array of values? – Ander Biguri Aug 03 '17 at 13:27
  • I have an array of values and want to find local maxima for those values. What I have to do I have to find Local maximas and make my signal flat at all local maximas . So I have set of array which contains values for my signal and want to calculate local maximas for that. – Peter Aug 03 '17 at 13:35

1 Answers1

2

Local maximums will always have this characteristic:

1) The prior point will always be less. 2) The following point will always be less.

So you could do something like this:

% Example input vector a
a = [0 1 2 1 2 3 2 1 0 1 2 1 5 2 1 4 1];

% Shift vectors so that prior point and next point lines up with
% current point. 
prior_points   = a(1:(length(a) - 2));
current_points = a(2:(length(a) - 1));
next_points    = a(3:length(a));

% Identify indices where slope is increasing from prior to current
% and decreasing from current to next.
prior_slope_positive = ((current_points  - prior_points)    > 0);
next_slope_negative  = ((next_points     - current_points)  <= 0);

% This will return indices of local maximas.  Note that the result
% is incremented by one since current_points was shifted once.
b = find(prior_slope_positive & next_slope_negative) + 1;

Note that this example does not include the first and last points as potential local maximas.

The local maximas in vector a are: Index Value 3 2 6 3 11 2 13 5 16 4

Therefore, at the conclusion vector b will be equal to: [3 6 11 13 16]

Jonathon S.
  • 1,928
  • 1
  • 12
  • 18
  • thanks a lot Jonathon S let me try this then i will get back to u . Thanks – Peter Aug 03 '17 at 13:42
  • Jonathon what do u mean by (a) here becaue this code is giving me just increasing values of local maximas when i put my data but for my case i have local maximas on decreasing trends also so in this case i am only getting linear curve – Peter Aug 03 '17 at 13:56
  • Peter. I updated my answer for clarity and included an example. Vector a is what I am using as the input vector and vector b will contain a list of indices for the local maxima. – Jonathon S. Aug 03 '17 at 14:14
  • Thanks a lot that worked perfect and one last thing this code is giving me location of local maximas but f I want to find values of local maximas because i need those also. – Peter Aug 03 '17 at 14:24
  • Just use the following: `c = a(b);` This will set `c` equal to the value of the maximas in `a`. It basically indexes `a` with all of the indices stored in `b`. – Jonathon S. Aug 03 '17 at 14:26
  • I have got the link so i can consult that thanks a lot for ur help .. – Peter Aug 03 '17 at 14:28
  • Thanks a lot for update it worked great... It worked like a piece of cake thanks again for help – Peter Aug 03 '17 at 14:43