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

1 view (last 30 days)
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 Comments
Jorge Montane
Jorge Montane on 17 Jan 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].

Sign in to comment.

Accepted Answer

Birdman
Birdman on 17 Jan 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

More Answers (1)

Stephen23
Stephen23 on 17 Jan 2018
Edited: Stephen23 on 17 Jan 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

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!