当想用一个矩阵的数据 除以另一个矩阵的对应位置的数值时,可以直接用两个矩阵相除,例子如下
>a<-matrix(c(2,0,2,0),nrow = 2)
> a
[,1] [,2]
[1,]...2 2
[2,] 0 0
> b<-matrix(c(0.5,0,0,1),nrow = 2)
> b
[,1] [,2]
[1,] 0.5 0
[2,] 0.0...> c<-a/b
> c[a==0|b==0]<-0
> c
[,1] [,2]
[1,] 4 0
[2,] 0 0
参考:https://www.jianshu.com