useR's, I want to be able to rotate a matrix 90 degrees, clockwise. For example,> mat[,1] [,2] [,3] [,1] 1 2 1 [,2] 3 2 6 [,3] 4 5 3 I want to rotate it, so that it looks like this... [,1] [,2] [,3] [,1] 4 3 1 [,2] 5 2 2 [,3] 3 6 1 Does anyone know a quick and straightforward way to do this? Thanks in advance. -- View this message in context: http://n4.nabble.com/can-I-rotate-a-matrix-tp1598469p1598469.html Sent from the R help mailing list archive at Nabble.com.
Try this: t(mat[3:1,]) On Thu, Mar 18, 2010 at 4:10 PM, dc896148 <cyr.derek at gmail.com> wrote:> > useR's, > I want to be able to rotate a matrix 90 degrees, clockwise. ?For example, >> mat > ? ? [,1] [,2] [,3] > [,1] ? 1 ? ?2 ? ? 1 > [,2] ? 3 ? ?2 ? ? 6 > [,3] ? 4 ? ?5 ? ? 3 > > I want to rotate it, so that it looks like this... > ? ? [,1] [,2] [,3] > [,1] ? 4 ? ?3 ? ? 1 > [,2] ? 5 ? ?2 ? ? 2 > [,3] ? 3 ? ?6 ? ? 1 > > Does anyone know a quick and straightforward way to do this? > > Thanks in advance. > -- > View this message in context: http://n4.nabble.com/can-I-rotate-a-matrix-tp1598469p1598469.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Not sure why you are doing it, but you can do it like this: m = matrix(c(1,3,4,2,2,5,1,6,3), nrow =3) [,1] [,2] [,3] [1,] 1 2 1 [2,] 3 2 6 [3,] 4 5 3 t(m)[ , ncol(m):1] [,1] [,2] [,3] [1,] 4 3 1 [2,] 5 2 2 [3,] 3 6 1 I hope that helps. Greg On 3/18/10 12:10 PM, dc896148 wrote:> useR's, > I want to be able to rotate a matrix 90 degrees, clockwise. For example, > >> mat >> > [,1] [,2] [,3] > [,1] 1 2 1 > [,2] 3 2 6 > [,3] 4 5 3 > > I want to rotate it, so that it looks like this... > [,1] [,2] [,3] > [,1] 4 3 1 > [,2] 5 2 2 > [,3] 3 6 1 > > Does anyone know a quick and straightforward way to do this? > > Thanks in advance. >-- Greg Hirson ghirson at ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616
I belive that> apply(t(mat),2,rev)[,1] [,2] [,3] [1,] 1 6 3 [2,] 2 2 5 [3,] 1 3 4 will do what you want. I'll leave it up to you to decide whether it's straightforward. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 18 Mar 2010, dc896148 wrote:> > useR's, > I want to be able to rotate a matrix 90 degrees, clockwise. For example, >> mat > [,1] [,2] [,3] > [,1] 1 2 1 > [,2] 3 2 6 > [,3] 4 5 3 > > I want to rotate it, so that it looks like this... > [,1] [,2] [,3] > [,1] 4 3 1 > [,2] 5 2 2 > [,3] 3 6 1 > > Does anyone know a quick and straightforward way to do this? > > Thanks in advance. > -- > View this message in context: http://n4.nabble.com/can-I-rotate-a-matrix-tp1598469p1598469.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On 19/03/2010, at 8:10 AM, dc896148 wrote:> > useR's, > I want to be able to rotate a matrix 90 degrees, clockwise. For example, >> mat > [,1] [,2] [,3] > [,1] 1 2 1 > [,2] 3 2 6 > [,3] 4 5 3 > > I want to rotate it, so that it looks like this... > [,1] [,2] [,3] > [,1] 4 3 1 > [,2] 5 2 2 > [,3] 3 6 1 > > Does anyone know a quick and straightforward way to do this?Does t(mat[nrow(mat):1,]) float your boat? cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
On 18-Mar-10 19:10:46, dc896148 wrote:> useR's, > I want to be able to rotate a matrix 90 degrees, clockwise. > For > example, >> mat > [,1] [,2] [,3] > [,1] 1 2 1 > [,2] 3 2 6 > [,3] 4 5 3 > > I want to rotate it, so that it looks like this... > [,1] [,2] [,3] > [,1] 4 3 1 > [,2] 5 2 2 > [,3] 3 6 1 > > Does anyone know a quick and straightforward way to do this? > > Thanks in advance. > --mat # [,1] [,2] [,3] # [1,] 1 2 1 # [2,] 3 2 6 # [3,] 4 5 3 matrix(rev(mat),nrow=3,byrow=TRUE)[(3:1),] # [,1] [,2] [,3] # [1,] 4 3 1 # [2,] 5 2 2 # [3,] 3 6 1 How's that? (But straightforward? Straightbackward, more like). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Mar-10 Time: 19:57:07 ------------------------------ XFMail ------------------------------