This may be a classic problem but I couldn't find a solution in the
lists so far. I'm trying to run a filter on radar data, where a pixels
result value is predicted as weighted average of the values in a 3x3
neighborhood in the current time step.
For one time step the operation could look like
filter<-matrix(rep(1/9,9),3)
[,1] [,2] [,3]
[1,] 0.1111111 0.1111111 0.1111111
[2,] 0.1111111 0.1111111 0.1111111
[3,] 0.1111111 0.1111111 0.1111111
base<-matrix(runif(100,0.0,2),10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[,8] [,9] [,10]
[1,] 1.2748495 1.64100480 1.7629353 1.04471073 0.17636041 0.7920916
1.81948900 0.28150594 0.40615944 0.33799927 [2,] 0.9134852 1.04947132
1.7847194 0.28143589 0.25744083 1.4310364
1.48369795 1.04006404 0.51538211 1.54781491 [3,] 1.4310985 1.01676632
1.8475540 1.97060630 0.01470109 0.2014272
1.79969115 1.04854042 0.90953386 0.02998074 [4,] 1.7304672 0.93514790
0.1203939 0.69291092 0.34618531 1.5409637
0.58926745 1.61994100 1.66063354 0.69013138 [5,] 0.7535980 0.64312043
1.2213976 0.03293221 1.70696788 1.6062746
0.06455516 0.62512456 1.94994886 0.52073311 [6,] 1.6559531 0.35279738
0.5878466 1.99312250 1.98113265 0.4076653
1.60588111 1.77600453 0.73479768 1.99889307 [7,] 0.1142795 0.22586611
0.2603767 0.33145144 1.37324328 0.8435238
0.78651141 0.46745984 0.93032790 1.20699560 [8,] 1.9036901 1.71863581
0.6056311 1.83853173 1.82158619 0.3230073
0.17650283 0.02147692 0.02384052 1.56027144 [9,] 1.8832814 1.38824498
0.7263898 1.63432910 1.65317872 1.4431693
1.06139030 1.08942924 0.15570795 1.21371980 [10,] 0.5374888 0.03651226
1.3404380 1.23004404 1.99562437 1.4025763
1.35752389 0.39377822 1.03457161 0.90385665
result<-matrix(ncol=8,nrow=8)
for (i in c(2:9)){
for (j in c(2:9)){
result[(i-1),(j-1)]<-sum(base[(i-1):(i+1),(j-1):(j+1)]*filter)
}
}
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[,8]
[1,] 1.4135427 1.3776893 1.0156071 0.6855345 0.8862151 1.0997271
1.0337849
0.6796645
[2,] 1.2032338 1.0776673 0.8128831 0.7485231 0.8516012 1.1949588
1.1851946
1.0068913
[3,] 1.0777271 0.9423144 0.8837388 0.9014410 0.8744482 1.0106428
1.1408040
1.0060631
[4,] 0.8889691 0.7310744 0.9647655 1.1453506 1.0943215 1.0928531
1.1806838
1.2862453
[5,] 0.6461373 0.6276568 1.0542745 1.1418126 1.1528617 0.9092223
0.9934012
1.1344761
[6,] 0.8250085 0.8793621 1.1992136 1.2125849 1.0354504 0.7120037
0.7247559
0.9688964
[7,] 0.9807106 0.9699396 1.1383020 1.2513357 1.0535681 0.6902746
0.5236274
0.7410255
[8,] 1.1267014 1.1687508 1.4273059 1.4824497 1.2482844 0.8076505
0.5904691
0.7107391
I will have to run this operation about 300.000 times in a parameter
estimation routine, so I was wondering if anyone could point me at an
efficient way of filtering that avoids the nested for loop.
Thanks.
Roland