Skip to content
Prev 81525 / 398502 Next

Matrix rotation

Try this:

# cyclically rotate a vector to the right
rot <- function(x) c(x[length(x)], x[-length(x)])

# create a vector from a 2x2 matrix moving left to right along
# first row and then right to left along second row
m2v <- function(m) m[c(1,3,4,2)]

# inverse of m2v.  Note that c(1,4,2,3) equals order(c(1,3,4,2))
v2m <- function(v) matrix(v[c(1,4,2,3)],2)

m <- matrix(1:4, 2, byrow = TRUE)
print(mm <- m)
for(i in 1:4) print(mm <- v2m(rot(m2v(mm))))
On 11/24/05, Benjamin Lloyd-Hughes <blh at cpom.ucl.ac.uk> wrote: