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 <[EMAIL PROTECTED]> wrote: > Ok I warned you that I'd been drinking! What I really meant was > something to go from: > > [,1] [,2] > [1,] 1 2 > [2,] 4 3 > > to > > [,1] [,2] > [1,] 4 1 > [2,] 3 2 > > to > > [,1] [,2] > [1,] 3 4 > [2,] 2 1 > > to > > [,1] [,2] > [1,] 2 3 > [2,] 1 4 > > Sorry for being a muppet, B > > > Begin forwarded message: > > > From: Benjamin Lloyd-Hughes <[EMAIL PROTECTED]> > > Date: 24 November 2005 16:15:58 GMT > > To: [email protected] > > Subject: Matrix rotation > > > > Dearest All, > > > > Ok so I've had a couple of glasses of wine over lunch today... This is > > likely to be trivial but I'm struggling to find a more elegant way to > > obtain the following matrix rotations: > > > > > M <- matrix(c(1,0,0,0), ncol=2) > > > M > > [,1] [,2] > > [1,] 1 0 > > [2,] 0 0 > > > N <- abind(M[2,],M[1,],along=2) > > > N > > [,1] [,2] > > [1,] 0 1 > > [2,] 0 0 > > > P <- abind(N[2,],N[1,],along=2) > > > P > > [,1] [,2] > > [1,] 0 0 > > [2,] 0 1 > > > Q <- abind(P[,2],P[,1],along=2) > > > Q > > [,1] [,2] > > [1,] 0 0 > > [2,] 1 0 > > > > And, more generally wish to rotate a n-dimensional data cube about > > some specified axis. > > > > Cheers, Ben > > > > [[alternative text/enriched version deleted]] > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
