How to obtain an array of maxima from a moving maximum?

3 visualizaciones (últimos 30 días)
JakeE10
JakeE10 el 3 de Ag. de 2018
Comentada: Akshay Dighe el 3 de Nov. de 2019
I would like to get the moving maximum of an array, however I would like the output array to be the same length as the input array and also only place the maximum value for any given window in the same index it was found in the input array. If the next window has the same maximum data point, I would like to input a zero for that index in the output array. An example is below:
A = [4 8 6 1 2 3 1 9 4 5]; % Input data array
windowLength = 3; % Window length for the moving maximum.
The desired output from the example above is:
M = [0 8 6 0 0 3 0 9 0 0];
I tried using both dsp.MovingMaximum and movmax but neither the desired output array.

Respuesta aceptada

Image Analyst
Image Analyst el 3 de Ag. de 2018
They don't work because you're wanting a specialized operation. Here is a way that works:
A = [4 8 6 1 2 3 1 9 4 5]; % Input data array
output = zeros(1, length(A));
windowLength = 3; % Window length for the moving maximum.
for k = 1 : length(A) - windowLength + 1
thisWindow = A(k : k + windowLength - 1)
windowMax = max(thisWindow)
for k2 = k : k + windowLength - 1
if thisWindow(k2-k+1) == windowMax
output(k2) = A(k2);
end
end
end
output % Show in command window.
  1 comentario
JakeE10
JakeE10 el 6 de Ag. de 2018
That works well to achieve the desired output, however it runs slowly for large arrays (i.e. 20E6 or more points). Is there an easy was to speed this process up? I know parfor loops will not work because of the indexing that goes on when searching the array, but I was not sure if there are other useful tools I do not know about within MATLAB.
Thank you.

Iniciar sesión para comentar.

Más respuestas (1)

Akshay Dighe
Akshay Dighe el 3 de Nov. de 2019
Hi,
What does the movmax function matlab actually does?
I am not understanding why it is used, please help!
  3 comentarios
Akshay Dighe
Akshay Dighe el 3 de Nov. de 2019
Thank you for the answer!
I have as question given by my professor. But I am unable to understand it and do the coding. I understood the theory but to convert it into the code seems difficult.
Can you help me out?
Here is the question1.png

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by