What is the mechanism or math behind the function islocalmax(A)?
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sim
el 8 de Nov. de 2023
Comentada: Sim
el 17 de Nov. de 2023
An example:
x = 1:100;
A = (1-cos(2*pi*0.01*x)).*sin(2*pi*0.15*x);
TF = islocalmax(A);
plot(x,A,x(TF),A(TF),'r*')
Also, if I open islocalmax in Matlab
>> open islocalmax
I just get the following code (so not additional information):
% Copyright 2017-2022 The MathWorks, Inc.
if nargout > 1
[tf, P] = matlab.internal.math.isLocalExtrema(A, true, varargin{:});
else
tf = matlab.internal.math.isLocalExtrema(A, true, varargin{:});
end
Respuesta aceptada
John D'Errico
el 8 de Nov. de 2023
Editada: John D'Errico
el 8 de Nov. de 2023
Someone who is not an employee of TMW cannot see the code. (I can't, as I am not an employee.) And if someone is an employee, they generally do not give specifics of code that is not an m-file. But, this is one of those simple codes that are mainly simple heuristics, an ad hoc algorithm that gets the job done. Nothing more is needed. And a basic rule when writing code is that if a simple code is sufficient, then just write the simple code. Get the job done, and move onto more difficult problems.
help islocalmax
As such, the islocalmax function detects if both immediate neighbors of an element are less than the element in the middle. So, without any parameters set, all that is required should be a tiny amount of excess.
A little experimentation should be sufficient to determine the basic rules the code will follow.
V = [4 2 2 3 1 2];
islocalmax(V)
So only the number 3 was flagged. An element at the end can not be a local max, by that definition, since the immediate elements on either side are not less than it.
This next test shows that an inequality is required. All elements of a constant vector are not local maxima.
V = [1 1 1 1 1 1 1];
islocalmax(V)
How about a case where there is an internal region that is greater than the neightbors, but is constant? We should see three local maxima in this next test. Only the third maximum is a true extremum in my eyes, because the first two cases are not distiinct from the immediate neighbors. But the other two cases still would seem to qualify, depending on how the inequality test is structured.
V = [1 2 2 1 3 3 3 1 3 4 3 1];
islocalmax(V)
Next, remember that we cannot have a local maximum at either end of a vector.
V = [0 0 0 1 1 1];
islocalmax(V)
A perturbation by eps is all that is required to introduce a max though.
V(5) = 1 + eps
islocalmax(V)
Unless, of course, one has decided to adjust the tolerance for a peak, using the "minprominence" property.
islocalmax(V,'minprominence',2*eps)
So all that does is adjust the test for inequality that is applied, using a tolerance.
Anyway, I would argue there is surely no serious mathematics behind this code, beyond a set of simple tests of inequality. There are several options in the code that will make those tests more complex, but still nothing sophisticated, just more complicated. Yes, I might apply diff to the vector to perform some of those tests, but diff is just a simple forward difference engine.
1 comentario
Más respuestas (1)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!