MATLAB: Efficient Weighted Moving average

moving averagetime series

I have a matrix with x, y, and an uncertainty value (25×3 double). I want to compute a moving average of these points weighted by the third value, (V(:,3)), with a time window of say 5 points. What is the most efficient way to achieve this?

Best Answer

Hi Matlab User
1.
simulating data
clear all,clc
V=zeros(25,3);x=V(:,1);y=V(:,2);
w=randi([1 9],25,1);
w=w/sum(w);V(:,3)=w
V(:,1)=randi([-10 10],25,1) % x
V(:,2)=randi([-10 10],25,1) % y
V(:,3)=w;
.
the data
format short
V =
-3.0000 3.0000 0.0857
-5.0000 8.0000 0.0190
-10.0000 3.0000 0.0476
-2.0000 -1.0000 0.0476
4.0000 1.0000 0.0571
0 3.0000 0.0190
1.0000 -7.0000 0.0190
10.0000 -1.0000 0.0857
10.0000 -1.0000 0.0762
-6.0000 -3.0000 0.0762
-5.0000 -9.0000 0.0381
0 4.0000 0.0095
-7.0000 1.0000 0.0190
-8.0000 -10.0000 0.0286
3.0000 8.0000 0.0381
-4.0000 -4.0000 0.0095
2.0000 6.0000 0.0190
-9.0000 0 0.0095
5.0000 6.0000 0.0286
-4.0000 -2.0000 0.0286
-10.0000 6.0000 0.0667
-9.0000 -2.0000 0.0381
-9.0000 1.0000 0.0095
7.0000 4.0000 0.0476
9.0000 -6.0000 0.0762
2.
averaging with the shifting window and applying the weight V(:,:,3)
N=25
R=zeros(N,1)
for k=3:1:N-2
s=[k-2 k-1 k k+1 k+2]
R(k)= mean(.5*(V(s,2)+V(s,1)).*V(s,3))
end
R =
0
0
-0.0133
-0.0076
-0.0248
0.0857
0.1686
0.0714
0.0124
0.0276
-0.0610
-0.1810
-0.0705
-0.0248
-0.0133
-0.0105
0.0724
0.0133
-0.0057
-0.0629
-0.0619
-0.0410
-0.0010
0
0
This does not apply the shifting window on the first 2 and last 2 sames, there aren't 5 samples to apply the window on head and tail anyway, aren't they?
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer
please click on the thumbs-up vote link
thanks in advance
John BG