Enlarge vector by putting average of surrounding numbers in between of every number of original vector

1 visualización (últimos 30 días)
How to make a longer vector by adding an additional element between neighboring elements in the original vector. Each new element should equal the average of its neighboring elements. x = [0 2 3 2 1 -1]
  3 comentarios
Jorge Montane
Jorge Montane el 17 de En. de 2018
my original vector is x = [0 2 3 2 1 -1]. And at the end I would need to see is x= [0 1 2 2.5 3 2.5 2 1.5 1 0 -1].

Iniciar sesión para comentar.

Respuesta aceptada

Birdman
Birdman el 17 de En. de 2018
x(3:2:2*numel(x)-1)=x(2:end);
for i=2:2:numel(x)-1
x(i)=(x(i-1)+x(i+1))/2
end

Más respuestas (1)

Stephen23
Stephen23 el 17 de En. de 2018
Editada: Stephen23 el 17 de En. de 2018
Without a loop:
>> x = [0 2 3 2 1 -1];
>> y = [x;mean([x(1:end-1);x(2:end)]),0];
>> y = y(1:end-1)
y =
0.00000 1.00000 2.00000 2.50000 3.00000 2.50000 2.00000 1.50000 1.00000 0.00000 -1.00000

Etiquetas

Aún no se han introducido etiquetas.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by