필터 지우기
필터 지우기

How to compute centered moving average from an NxM array

조회 수: 2 (최근 30일)
Poulomi Ganguli
Poulomi Ganguli 2020년 6월 7일
편집: Rik 2020년 6월 7일
Hello:
I have an 40x2 matrix, in which the first column shows day index and the 2nd column value for each day:
1 11
2 12
3 10
4 10
5 12
6 11
7 12
8 10
9 12
10 10
11 12
12 12
13 10
14 9
15 12
16 12
17 12
18 11
19 10
20 10
21 11
22 11
23 10
24 8
25 9
26 9
27 9
28 8
29 8
30 12
31 11
32 9
33 9
34 12
35 11
36 11
37 10
38 10
39 11
40 9
How to compute centered moving average with 30 days moving window taking 15th of the day as the midpoint and traversing backward 14 days and forward 15 days? Basically I want to compute: Xavg = median[X(i-14):X(i+15)]. where X(i) is the data point and Average is the averaging operator. I found a few thread using convolution operator but could not find out how to fix step size since my mid point is located on the 15th day and further how to compute median effect?

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 7일
You can use movmean() and discard the endpoints
M = [
1 11
2 12
3 10
4 10
5 12
6 11
7 12
8 10
9 12
10 10
11 12
12 12
13 10
14 9
15 12
16 12
17 12
18 11
19 10
20 10
21 11
22 11
23 10
24 8
25 9
26 9
27 9
28 8
29 8
30 12
31 11
32 9
33 9
34 12
35 11
36 11
37 10
38 10
39 11
40 9];
avg_val = movmean(M(:,2), 30, 'Endpoints', 'discard')
  댓글 수: 2
Poulomi Ganguli
Poulomi Ganguli 2020년 6월 7일
Hi, I am running MATLAB 2015 movmean operator is not available.
Ameer Hamza
Ameer Hamza 2020년 6월 7일
Alternative options
avg_val = conv(M(:,2), ones(1,30)/30, 'valid');
or
avg_val = filter(ones(1,30)/30, 1, M(:,2));
avg_val(1:29) = [];

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

추가 답변 (1개)

Rik
Rik 2020년 6월 7일
편집: Rik 2020년 6월 7일
You can use a convolution with a flat array as the second input.
avg_val = convn(M(:,2), ones(30,1)/30, 'valid');

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by