Automate an averaging process and store info into an array

조회 수: 1 (최근 30일)
zen 2022년 7월 4일
댓글: Keshav 2022년 7월 5일
I have a code that takes the average of every 2 points of an array by changing k:
mdata = [1:10];
n1 = 1;
k = 1;
for n = 1:length(mdata)
ma(n) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
std_ma = std(ma);
end
I want to be able to automate a process that increases the averaging size each time (ex: 2 points averaged each time of an array, then 3 points averaged each time of an array, 4 points averaged, … n points averaged) and then stores all of that averaged data into a single array.
For example, if we have 2000 data points and we take the average of every 2 points, we’d end up with 1x1000. Then if we do 3 points averaged, we get 1x666~, 4 averaged we get 1x500, etc. I want to automate this and have all of that information stored into a single array.
Not sure how to go about it, any help?

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

답변 (1개)

Keshav 2022년 7월 4일
Hi, Based on my understanding you want to first calculate the average of 2 elements, 3 elements,... n elements. so if the array is [1 2 3 .... 10] then you want to make an array ans such that
ans = [avg(1,2) avg(3,4) .... avg(9,10) avg(1,2,3) avg(4,5,6) avg(7,8,9) avg(1,2,3,4) ................... avg(1,2,3,4,....,10)]
as you have already written the code to find the average of two elements, I made it generalize for every possible k.
clc
clear
mdata = [1:10];
n1 = 1;
n = 1;
for k=1:length(mdata)-1
n1 = 1;
while n1+k <= length(mdata)
ma(n) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
ma
댓글 수: 2없음 표시없음 숨기기
zen 2022년 7월 4일
exactly what I wanted, thanks! one question though, I want to be able to log the info in regards to k, where I changed ma(n) to ma(n,k) and get a 17x9 array which is what I want, however the array includes a ton of unwanted zeroes. For example:
clc
clear
mdata = [1:10];
n1 = 1;
n = 1;
for k = 1:length(mdata) - 1
n1 = 1;
while n1 + k <= length(mdata)
ma(n,k) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
ma
ma = 17×9
1.5000 0 0 0 0 0 0 0 0 3.5000 0 0 0 0 0 0 0 0 5.5000 0 0 0 0 0 0 0 0 7.5000 0 0 0 0 0 0 0 0 9.5000 0 0 0 0 0 0 0 0 0 2.0000 0 0 0 0 0 0 0 0 5.0000 0 0 0 0 0 0 0 0 8.0000 0 0 0 0 0 0 0 0 0 2.5000 0 0 0 0 0 0 0 0 6.5000 0 0 0 0 0 0
I don't want any of the extra zeroes, just the values themselves in the same 17x9 formatted array. How would I go about this?
Keshav 2022년 7월 5일
you can use the below code to remove the extra zero. Just you have to reinitialize the value of n.
clc
clear
mdata = [1:10];
for k=1:length(mdata)-1
n1 = 1;
n = 1;
while n1+k <= length(mdata)
ma(n,k) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
ma
ma = 5×9
1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 3.5000 5.0000 6.5000 8.0000 0 0 0 0 0 5.5000 8.0000 0 0 0 0 0 0 0 7.5000 0 0 0 0 0 0 0 0 9.5000 0 0 0 0 0 0 0 0

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by