[R] can I rotate a matrix

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu Mar 18 22:00:31 CET 2010


On 18-Mar-10 20:25:22, Seeliger.Curt at epamail.epa.gov wrote:
>> > 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
>> > 
>> 
>>   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).
> 
> One might also work it every bit as straight backward as follows:
> z<-matrix(c(1,2,1,3,2,6,4,5,3), ncol=3, byrow=TRUE)
> t(z[3:1,3:1])[3:1,]
> 
> 
> -- 
> Curt Seeliger, Data Ranger
> Raytheon Information Services - Contractor to ORD
> seeliger.curt at epa.gov
> 541/754-4638

Now that *is* neat!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Mar-10                                       Time: 21:00:28
------------------------------ XFMail ------------------------------



More information about the R-help mailing list