how to keep first 3 datas and obtain average, then skip or ignore the next 3 data and keep next 3 datas again....??

2 views (last 30 days)
Hi all.
I have a 216 data in a one column from excel. but i want to keep first 3 datas and obtain average, then skip or ignore the next 3 data and keep next 3 datas again until finish 1:216, someone how to do that?

Accepted Answer

Thiago Henrique Gomes Lobato
An efficient way to do this is to create all the index that are going to belong to the average and then do it in a vectorized way. In your case, you can define each value belonging to the average with intervals of 6 values, an example can be done like this:
N = 15;
A = 1:N; % Simple vector to verify result, substitute by your A with N = 216
idx1 = 1:6:N;
idx2 = 2:6:N;
idx3 = 3:6:N;
Average = mean( [A(idx1);A(idx2);A(idx3)] )'
Average =
2
8
14
  3 Comments
Thiago Henrique Gomes Lobato
You use the same logic as my answer, in your exact case you can write
v1=dat(5:end,9);
N = size(v1,1);
idx1 = 1:6:N;
idx2 = 2:6:N;
idx3 = 3:6:N;
filteredv1=[v1(idx1),v1(idx2),v1(idx3)]';
filteredv1 = filteredv1(1:end)';
Averagev1 = mean( [v1(idx1),v1(idx2),v1(idx3)],2 );

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!