How to take the average every 4 data points?

115 views (last 30 days)
First of all I have an array of 9536x1.
I would like to calculate the average value of every 4 data points and put the resulting values into a new array.
I guess using loop is the best solution for my quesiton but my understand is lacking at the moment.
For example,
Function [] = flitering(mydataset);
n = 0:1:(length(mydataset)/4)
for i=n
mean(mydataset(i+1:i+32,1));
end
If I would like to process 2 arrays at once would it be:
Function [] = flitering(mydataset,mydataset2);
n = 0:1:(length(mydataset)/4)
for i=n
mean(mydataset(i+1:i+32,1));
mean(mydataset2(i+1:i+32,1));
end

Accepted Answer

Star Strider
Star Strider on 14 Nov 2022
An easier way is to just reshape the vector to a matrix, and then take the mean of the columns —
v = (1:9536).';
v = 9536×1
1 2 3 4 5 6 7 8 9 10
vm = reshape(v, 4, [])
vm = 4×2384
1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 82 86 90 94 98 102 106 110 114 118 3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 67 71 75 79 83 87 91 95 99 103 107 111 115 119 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120
vMean4 = mean(vm)
vMean4 = 1×2384
2.5000 6.5000 10.5000 14.5000 18.5000 22.5000 26.5000 30.5000 34.5000 38.5000 42.5000 46.5000 50.5000 54.5000 58.5000 62.5000 66.5000 70.5000 74.5000 78.5000 82.5000 86.5000 90.5000 94.5000 98.5000 102.5000 106.5000 110.5000 114.5000 118.5000
In the event that the number of elements in the vector is not an exact multiple of 4:
v2 = (1:9535).';
cols = fix(numel(v2)/4)
cols = 2383
v2m = reshape(v2(1:4*cols),4,[]);
v2m = 4×2383
1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 82 86 90 94 98 102 106 110 114 118 3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 67 71 75 79 83 87 91 95 99 103 107 111 115 119 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120
v2Mean4 = mean(v2m)
v2Mean4 = 1×2383
2.5000 6.5000 10.5000 14.5000 18.5000 22.5000 26.5000 30.5000 34.5000 38.5000 42.5000 46.5000 50.5000 54.5000 58.5000 62.5000 66.5000 70.5000 74.5000 78.5000 82.5000 86.5000 90.5000 94.5000 98.5000 102.5000 106.5000 110.5000 114.5000 118.5000
v2(4*cols+1:end)
ans = 3×1
9533 9534 9535
v2Mean4(end+1) = mean(v2(4*cols+1:end))
v2Mean4 = 1×2384
2.5000 6.5000 10.5000 14.5000 18.5000 22.5000 26.5000 30.5000 34.5000 38.5000 42.5000 46.5000 50.5000 54.5000 58.5000 62.5000 66.5000 70.5000 74.5000 78.5000 82.5000 86.5000 90.5000 94.5000 98.5000 102.5000 106.5000 110.5000 114.5000 118.5000
Check = v2Mean4(end)
Check = 9534
.
  3 Comments
MJ
MJ on 14 Nov 2022
Thanks for another great advice Star Strider!
I will try this method as well!

Sign in to comment.

More Answers (4)

William Rose
William Rose on 14 Nov 2022
I assume that you want a function that returns the average of points 1-4, then the average of points 5-8, then the average of points 9-12, and so on.
function y = filtering(x)
%FILTERING Compute 4-point moving average without overlap
n=floor(length(x)/4);
y=zeros(1,n);
for i=1:n
y(i)=sum(x(4*i-3:4*i))/4;
end
end
The floor() function enables filtering() to work without error, if the input vector has a length that is not a multiple of 4.
Example of usage:
>> x=sin(2*pi*(0:99)/100)+randn(1,100)/4;
>> y=filtering(x);
>> subplot(2,1,1); plot(x); subplot(2,1,2); plot(y)
It makes the figure below. The uppper plot is the unfiltered signal. The bottom plot is the filtered signal.
Your results may vary, since randn() produces different random numbers with each call.
You can adjust the script as you wish, to make it process two vectors with a single call.
Good luck with your work.
  1 Comment
MJ
MJ on 14 Nov 2022
Dear William,
I truly appreciate your help.
It did solve my problem!
Kind regards

Sign in to comment.


Khushboo
Khushboo on 14 Nov 2022
Hello,
You can try out the following instead of using a for loop:
n = 4; % calculate mean after every 4th data point
a = arrayfun(@(i) mean(mydataset(i:i+n-1)),1:n:length(mydataset)-n+1)'; % resulting vector
b = arrayfun(@(i) mean(mydataset2(i:i+n-1)),1:n:length(mydataset2)-n+1)';
Hope this helps!

Askic V
Askic V on 14 Nov 2022
Edited: Askic V on 14 Nov 2022
Just in case if you want to calculate mean/average of the elements in the last chunk (partition) that contains less that 4 elements, I would suggest the following code (example where last chunk contains 3 elements):
function mean_array = mean_chunk_array(arr, chunk_size)
chunk_size = 4;
nr_divisions = ceil(length(arr)/chunk_size);
mean_array = zeros(nr_divisions,1);
for ii = 0:nr_divisions-1
end_point = (ii+1)*chunk_size;
if end_point > length(arr)
end_point = length(arr);
end
mean_array(ii+1) = mean(arr(ii*chunk_size+1: end_point));
end
end
and call it like this:
arr = 1:27;
chunk_size = 4;
mean_arr = mean_chunk_array(arr, chunk_size)

Delprat Sebastien
Delprat Sebastien on 24 Jun 2024
There is a smooth function y=smooth(x,4)...
Simple and should be enough.

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!