R语言中矩阵逆序转置代码解释
一年前都记得,这次要修改,忘了,所以决定记下了。
PlotM<- function(Z,heatc=12) {
m<-ncol(Z);
n<-nrow(Z);
MM<-matrix(0,m,n);
MM[1:m,]<-t(Z[,1:m][n:1,]);
image(MM,axes=FALSE,col=heat.colors(heatc));
}
下面是解释
PlotM<- function(Z,heatc=12) {
m<-ncol(Z); #矩阵Z的行数
n<-nrow(Z); #矩阵Z的列数
MM<-matrix(0,m,n); #构建一个元素全零的MM矩阵
MM[1:m,]<-t(Z[,1:m][n:1,]); #将Z按列逆序排列后,转置后复制给MM矩阵
image(MM,axes=FALSE,col=heat.colors(heatc)); #画图,没有坐标轴注释,颜色选用heat.colors,将色带分为12份。
}
运行模拟效果
ZZ<-matrix(1:8,nrow=4,ncol=2)
ZZ [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 |
MM [,1] [,2] [,3] [,4] [1,] 4 3 2 1[2,] 8 7 6 5 |
||
直接画ZZ image(ZZ,axes=FALSE,col=heat.colors(12)) |
|||
经过函数PlotM的、 PlotM(ZZ) |