ng, I have a matrix (x) with binary content. Each row of the matrix holds exactly one 1, and the rest of the row is zeros. The thing is that I need to 'collapse' the matrix to one column where each row holds the original column index of the 1's (y). Sometimes, the matrix is quite large, so I have a perfomance problem. x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0),ncol=3,byrow=T) x [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 0 1 [3,] 0 1 0 [4,] 0 0 1 [5,] 0 1 0 [6,] 1 0 0 In the matrix above, on the first row, the 1 is in column 1, hence '1' on the first row in the matrix below. On the second row in the matrix above, the 1 is in column 3, hence the '3' on the second row in the matrix below. And so on... y [,1] [1,] 1 [2,] 3 [3,] 2 [4,] 3 [5,] 2 [6,] 1
try this: x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0), ncol = 3, byrow = TRUE) which(x == 1, arr.ind = TRUE)[, "col", drop = FALSE] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <stefan.petersson at inizio.se> To: <r-help at r-project.org> Sent: Wednesday, June 11, 2008 10:10 AM Subject: [R] Matrix transformation problem> > ng, > > I have a matrix (x) with binary content. Each row of the matrix > holds exactly one 1, and the rest of the row is zeros. The thing is > that I need to 'collapse' the matrix to one column where each row > holds the original column index of the 1's (y). Sometimes, the > matrix is quite large, so I have a perfomance problem. > > x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, > 1,0,0),ncol=3,byrow=T) > x > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 0 1 > [3,] 0 1 0 > [4,] 0 0 1 > [5,] 0 1 0 > [6,] 1 0 0 > > In the matrix above, on the first row, the 1 is in column 1, hence > '1' on the first row in the matrix below. On the second row in the > matrix above, the 1 is in column 3, hence the '3' on the second row > in the matrix below. And so on... > > y > [,1] > [1,] 1 > [2,] 3 > [3,] 2 > [4,] 3 > [5,] 2 > [6,] 1 > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
sorry, my previous answer was not correct; you need: x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0), ncol = 3, byrow = TRUE) which(t(x == 1), arr.ind = TRUE)[, "row", drop = FALSE] Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <stefan.petersson at inizio.se> To: <r-help at r-project.org> Sent: Wednesday, June 11, 2008 10:10 AM Subject: [R] Matrix transformation problem> > ng, > > I have a matrix (x) with binary content. Each row of the matrix > holds exactly one 1, and the rest of the row is zeros. The thing is > that I need to 'collapse' the matrix to one column where each row > holds the original column index of the 1's (y). Sometimes, the > matrix is quite large, so I have a perfomance problem. > > x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, > 1,0,0),ncol=3,byrow=T) > x > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 0 1 > [3,] 0 1 0 > [4,] 0 0 1 > [5,] 0 1 0 > [6,] 1 0 0 > > In the matrix above, on the first row, the 1 is in column 1, hence > '1' on the first row in the matrix below. On the second row in the > matrix above, the 1 is in column 3, hence the '3' on the second row > in the matrix below. And so on... > > y > [,1] > [1,] 1 > [2,] 3 > [3,] 2 > [4,] 3 > [5,] 2 > [6,] 1 > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
you may try a matrix multiplication, which has a very high performance in R x%*%1:ncol(x) hth. stefan.petersson at inizio.se schrieb:> ng, > > I have a matrix (x) with binary content. Each row of the matrix holds exactly one 1, and the rest of the row is zeros. The thing is that I need to 'collapse' the matrix to one column where each row holds the original column index of the 1's (y). Sometimes, the matrix is quite large, so I have a perfomance problem. > > x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0),ncol=3,byrow=T) > x > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 0 1 > [3,] 0 1 0 > [4,] 0 0 1 > [5,] 0 1 0 > [6,] 1 0 0 > > In the matrix above, on the first row, the 1 is in column 1, hence '1' on the first row in the matrix below. On the second row in the matrix above, the 1 is in column 3, hence the '3' on the second row in the matrix below. And so on... > > y > [,1] > [1,] 1 > [2,] 3 > [3,] 2 > [4,] 3 > [5,] 2 > [6,] 1 > > ______________________________________________ > 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790
stefan.petersson wrote:> > > ng, > > I have a matrix (x) with binary content. Each row of the matrix holds > exactly one 1, and the rest of the row is zeros. The thing is that I need > to 'collapse' the matrix to one column where each row holds the original > column index of the 1's (y). Sometimes, the matrix is quite large, so I > have a perfomance problem. > > x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0),ncol=3,byrow=T) > x > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 0 1 > [3,] 0 1 0 > [4,] 0 0 1 > [5,] 0 1 0 > [6,] 1 0 0 > > In the matrix above, on the first row, the 1 is in column 1, hence '1' on > the first row in the matrix below. On the second row in the matrix above, > the 1 is in column 3, hence the '3' on the second row in the matrix below. > And so on... > > y > [,1] > [1,] 1 > [2,] 3 > [3,] 2 > [4,] 3 > [5,] 2 > [6,] 1 > >max.col(x) -- View this message in context: http://www.nabble.com/Matrix-transformation-problem-tp17773270p17779431.html Sent from the R help mailing list archive at Nabble.com.