How to take the average every 4 data points?
조회 수: 115 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2022년 11월 14일
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
추가 답변 (4개)
William Rose
2022년 11월 14일
@민수,
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
Khushboo
2022년 11월 14일
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
Askic V
2022년 11월 14일
편집: Askic V
2022년 11월 14일
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
Delprat Sebastien
2024년 6월 24일
There is a smooth function y=smooth(x,4)...
Simple and should be enough.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!