How to take the average every 4 data points?

조회 수: 175 (최근 30일)
민수
민수 2022년 11월 14일
댓글: Star Strider 2022년 11월 14일
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

채택된 답변

Star Strider
Star Strider 2022년 11월 14일
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
MJ
MJ 2022년 11월 14일
Thanks for another great advice Star Strider!
I will try this method as well!
Star Strider
Star Strider 2022년 11월 14일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

William Rose
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
MJ
MJ 2022년 11월 14일
Dear William,
I truly appreciate your help.
It did solve my problem!
Kind regards

댓글을 달려면 로그인하십시오.


Khushboo
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!

Askic V
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)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by