How to compute centered moving average from an NxM array

3 次查看(过去 30 天)
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 个评论
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