且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R图像()绘制矩阵旋转?

更新时间:2023-12-01 11:10:34

你可以反转矩阵,然后转置。

  mat1<  -  apply(mat1,2,rev)
图像(1:3,1:3,t(mat1))

因为它绘制而令人困惑从下到上按行排列,但R按列向下索引矩阵。因此,第一行中从左到右的像素对应于矩阵中的第一列,自上而下。


I've been reading the docs for R image() but I don't get it. Why does this matrix:

> mat1
     [,1] [,2] [,3]
[1,]    1    0    1
[2,]    0    1    0
[3,]    0    0    0

Plotted like this:

> image(c(1:3), c(1:3), mat1)

yield this:

And how can I make the layout the same as the printed matrix? It's not a matter of just taking the transpose to flip x and y, as that ends up with an 'upside down' image.

You could reverse the matrix, then transpose.

mat1 <- apply(mat1, 2, rev)
image(1:3, 1:3, t(mat1))

It's confusing because it draws by row from bottom up, but R indexes matrices by column, top down. So, the pixels in the first row, from left to right, correspond to the first column in the matrix, top down.