maximum of elements

Hello,
I have a dataset 'a' with 2000 rows and 1 column. I want to create a new dataset 'b' which contains values that are the maximum of the 10 previous rows of 'a'. So row 11 of dataset 'b' has to contain the maximum value of rows 1-10 of dataset 'a' How can I program this?
Thanks, Pieter

 Respuesta aceptada

Arnaud Miege
Arnaud Miege el 5 de Abr. de 2011

0 votos

There may be a cleaner way of doing this the following should work:
b = zeros(size(a));
b(1:10) = a(1:10);
for k=11:length(a)
b(k) = max(a(k-10:k-1));
end
HTH,
Arnaud

1 comentario

Pieter
Pieter el 5 de Abr. de 2011
Thanks!
Exactly what I was searching for!
Pieter

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 5 de Abr. de 2011

1 voto

NLFILTER of Matlab's Image Processing Toolbox can calculate the moving max:
fun = @(x) max(x(:));
b = nlfilter(a, [10, 1], fun);
Do you have the Image Processing Toolbox?

1 comentario

Jan
Jan el 5 de Abr. de 2011
Do not use this! It is more than 1000 times slower than the BUFFER method of my other answer.

Iniciar sesión para comentar.

Jan
Jan el 5 de Abr. de 2011

1 voto

If your input is not too large (2000 is no problem), you can expand the data using BUFFER. This is 1300 times faster than NLFILTER and much faster than the loop:
M = max(buffer(a, 10, 9, 'nodelay'))
M = M(:); % BUFFER replies a row vector
b = [a(1:10); M(1:end - 1)];

Etiquetas

Preguntada:

el 5 de Abr. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by