Created
September 23, 2015 19:57
-
-
Save mbq/62eac0d83b6755279f75 to your computer and use it in GitHub Desktop.
Fast 9-point mean matrix smoothing in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s9<-function(M,iters=1){ | |
M*0+9->n; | |
n[,1]<-n[1,]<-n[,ncol(n)]<-n[nrow(n),]<-6; | |
n[1,1]<-n[1,ncol(n)]<-n[nrow(n),1]<-n[nrow(n),ncol(n)]<-4; | |
for(e in 1:iters){ | |
s<-M+ | |
cbind(M[,-1],0)+ | |
cbind(0,M[,-ncol(M)])+ | |
rbind(M[-1,],0)+ | |
rbind(0,M[-nrow(M),])+ | |
rbind(cbind(M[,-1],0)[-1,],0)+ | |
rbind(0,cbind(M[,-1],0)[-nrow(M),])+ | |
rbind(cbind(0,M[,-ncol(M)])[-1,],0)+ | |
rbind(0,cbind(0,M[,-ncol(M)])[-nrow(M),]); | |
s/n->M; | |
} | |
return(M); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment