Hi all,
  I have a 4 by 4 matrix, and I want to switch row 2 and row 3 first, then 
switch column 2 and column 3. Is there an easy way to do it?
The following is a tedious way to get what I want. But I wonder if there is a 
way to simplify this.

 > a=matrix(rnorm(16),4,4)
> a
            [,1]       [,2]        [,3]       [,4]
[1,]  0.33833811 -0.9422273 -0.06181611 -1.8346134
[2,] -0.68167996  0.2004836  0.71079887 -1.1590184
[3,]  0.07811688  0.1338694 -1.61262688  0.2988365
[4,]  0.21869786 -1.6356661 -0.13694344 -1.2121355
> b=a
> b[2,]=a[3,]
> b[3,]=a[2,]
> b
            [,1]       [,2]        [,3]       [,4]
[1,]  0.33833811 -0.9422273 -0.06181611 -1.8346134
[2,]  0.07811688  0.1338694 -1.61262688  0.2988365
[3,] -0.68167996  0.2004836  0.71079887 -1.1590184
[4,]  0.21869786 -1.6356661 -0.13694344 -1.2121355
> c=b
> c[,2]=b[,3]
> c[,3]=b[,2]
> c
            [,1]        [,2]       [,3]       [,4]
[1,]  0.33833811 -0.06181611 -0.9422273 -1.8346134
[2,]  0.07811688 -1.61262688  0.1338694  0.2988365
[3,] -0.68167996  0.71079887  0.2004836 -1.1590184
[4,]  0.21869786 -0.13694344 -1.6356661 -1.2121355


Sincerely,
Yanwei Zhang
Department of Actuarial Research and Modeling
Munich Re America
Tel: 609-275-2176
Email: [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]>


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to