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: yzhang@munichreamerica.com<mailto:yzhang@munichreamerica.com> [[alternative HTML version deleted]]
A <- matrix(rnorm(16)4,4) o <- c(1,3,2,4) A[o,o] -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Zhang Yanwei - Princeton-MRAm Sent: Thursday, August 07, 2008 10:31 AM To: r-help at r-project.org Subject: [R] Switch two rows in a matrix 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: yzhang at munichreamerica.com<mailto:yzhang at munichreamerica.com> [[alternative HTML version deleted]] ______________________________________________ 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. -------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
You could perhaps do it like that a<-a[c(1,2,4,3),] B. Zhang Yanwei - Princeton-MRAm wrote:> > 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: yzhang at munichreamerica.com<mailto:yzhang at munichreamerica.com> > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/Switch-two-rows-in-a-matrix-tp18872329p18872790.html Sent from the R help mailing list archive at Nabble.com.
sorry here the right thing a<-a[c(1,3,2,4),c(1,3,2,4)] B. Birgitle wrote:> > You could perhaps do it like that > > a<-a[c(1,2,4,3),] > > B. > > > Zhang Yanwei - Princeton-MRAm wrote: >> >> 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: yzhang at munichreamerica.com<mailto:yzhang at munichreamerica.com> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> >> > >----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/Switch-two-rows-in-a-matrix-tp18872329p18872854.html Sent from the R help mailing list archive at Nabble.com.
Hi Zhang> 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) '> b=a[c(2,1,3,4),] '> a [,1] [,2] [,3] [,4] [1,] -0.1954458 0.299055 -1.63830678 -0.9761293 [2,] -1.0558561 -1.225412 0.06439794 0.2919717 [3,] 0.3865482 0.682090 -0.52230467 -0.5961534 [4,] 1.7555946 -0.679426 -0.57409420 1.7552651 '> b [,1] [,2] [,3] [,4] [1,] -1.0558561 -1.225412 0.06439794 0.2919717 [2,] -0.1954458 0.299055 -1.63830678 -0.9761293 [3,] 0.3865482 0.682090 -0.52230467 -0.5961534 [4,] 1.7555946 -0.679426 -0.57409420 1.7552651