How to take the average every 4 data points?
115 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 14 Nov 2022
v = (1:9536).';
vm = reshape(v, 4, [])
vMean4 = mean(vm)
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)
v2m = reshape(v2(1:4*cols),4,[]);
v2Mean4 = mean(v2m)
v2(4*cols+1:end)
v2Mean4(end+1) = mean(v2(4*cols+1:end))
Check = v2Mean4(end)
.
3 Comments
More Answers (4)
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
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!
0 Comments
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)
0 Comments
Delprat Sebastien
on 24 Jun 2024
There is a smooth function y=smooth(x,4)...
Simple and should be enough.
0 Comments
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!