Storing data from looping and calculate again

So i have this data
data =
2
3
1
4
5
6
the calculation that i need is find average from last 6 data and storing the result as last data, and keep repeating until certain data, in this case im trying until 5 new data. so from that data, my new data should be
newdata=
2
3
1
4
5
6
3.5
3.75
3.875
4.35
4.41
im still stuck until this code
clc
data=[2;3;1;4;5;6]
for i=1:5
newdata(i)=mean(data);
end
newdata'

2 Comments

You want to find the mean of every five numbers out of given data? Is it?
Mas Biru
Mas Biru on 23 Mar 2017
Edited: Mas Biru on 23 Mar 2017
yes, find mean from last 6 data, until got 5 new data

Sign in to comment.

 Accepted Answer

Very simple way would be like this. If you want to apply this for much larger numerical array, then vectorization technique will be needed.
data=[2;3;1;4;5;6];
newdata = zeros(11,1);
newdata(1:6) = data;
for kk = 7:11
newdata(kk) = mean(newdata(kk-6:kk-1));
end

1 Comment

can you elaborate vectorization technique more? im still new on matlab :)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 23 Mar 2017

Commented:

on 23 Mar 2017

Community Treasure Hunt

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

Start Hunting!