Charles Novaes de Santana
2013-Jan-02 11:12 UTC
[R] In which column and in which row a number is in a matrix
Dear all, Happy New Year for all of you! I hope we have an year of essential freedom for everyone! I am trying to manipulate a matrix in order to know in which column and in which row a number is allocated. But, when we use the function "which" it returns the position of the number in the "vector representation of the matrix". For example:> mata<-matrix(2:7,nrow=2,ncol=3) > mata[,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7> matb<-matrix(2:7,nrow=3,ncol=2) > matb[,1] [,2] [1,] 2 5 [2,] 3 6 [3,] 4 7> which(mata==4)[1] 3> which(matb==4)[1] 3 The function "which" returns the position "3" for both, mata and matb, but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I wouldn't know that the number 4 is at the column 2 and row 1 of the mata and at the column 1 and row 3 of the matb. Do you know any way to know the column and the row of a number in a matrix automatically? Thanks, best regards, Charles -- Um ax?! :) -- Charles Novaes de Santana http://www.imedea.uib-csic.es/~charles PhD student - Global Change Laboratorio Internacional de Cambio Global Department of Global Change Research Instituto Mediterr?neo de Estudios Avanzados(CSIC/UIB) Calle Miquel Marques 21, 07190 Esporles - Islas Baleares - Espa?a Office phone - +34 971 610 896 Cell phone - +34 660 207 940
Gerrit Eichner
2013-Jan-02 11:21 UTC
[R] In which column and in which row a number is in a matrix
Hi, Charles, see ?which and learn about the argument arr.ind. Happy new year -- Gerrit On Wed, 2 Jan 2013, Charles Novaes de Santana wrote:> Dear all, Happy New Year for all of you! I hope we have an year of > essential freedom for everyone! > > I am trying to manipulate a matrix in order to know in which column > and in which row a number is allocated. But, when we use the function > "which" it returns the position of the number in the "vector > representation of the matrix". For example: > >> mata<-matrix(2:7,nrow=2,ncol=3) >> mata > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 >> matb<-matrix(2:7,nrow=3,ncol=2) >> matb > [,1] [,2] > [1,] 2 5 > [2,] 3 6 > [3,] 4 7 >> which(mata==4) > [1] 3 >> which(matb==4) > [1] 3 > > The function "which" returns the position "3" for both, mata and matb, > but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I > wouldn't know that the number 4 is at the column 2 and row 1 of the > mata and at the column 1 and row 3 of the matb. Do you know any way to > know the column and the row of a number in a matrix automatically? > > Thanks, best regards, > > Charles > > > -- > Um ax?! :) > > -- > Charles Novaes de Santana > http://www.imedea.uib-csic.es/~charles > PhD student - Global Change > Laboratorio Internacional de Cambio Global > Department of Global Change Research > Instituto Mediterr?neo de Estudios Avanzados(CSIC/UIB) > Calle Miquel Marques 21, 07190 > Esporles - Islas Baleares - Espa?a > > Office phone - +34 971 610 896 > Cell phone - +34 660 207 940 > > ______________________________________________ > 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.
HI, Try this: which(mata==4,arr.ind=TRUE) #???? row col #[1,]?? 1?? 2 ?which(matb==4,arr.ind=TRUE) #???? row col #[1,]?? 3?? 1 A.K. ----- Original Message ----- From: Charles Novaes de Santana <charles.santana at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, January 2, 2013 6:12 AM Subject: [R] In which column and in which row a number is in a matrix Dear all, Happy New Year for all of you! I hope we have an year of essential freedom for everyone! I am trying to manipulate a matrix in order to know in which column and in which row a number is allocated. But, when we use the function "which" it returns the position of the number in the "vector representation of the matrix". For example:> mata<-matrix(2:7,nrow=2,ncol=3) > mata? ? [,1] [,2] [,3] [1,]? ? 2? ? 4? ? 6 [2,]? ? 3? ? 5? ? 7> matb<-matrix(2:7,nrow=3,ncol=2) > matb? ? [,1] [,2] [1,]? ? 2? ? 5 [2,]? ? 3? ? 6 [3,]? ? 4? ? 7> which(mata==4)[1] 3> which(matb==4)[1] 3 The function "which" returns the position "3" for both, mata and matb, but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I wouldn't know that the number 4 is at the column 2 and row 1 of the mata and at the column 1 and row 3 of the matb. Do you know any way to know the column and the row of a number in a matrix automatically? Thanks, best regards, Charles -- Um ax?! :) -- Charles Novaes de Santana http://www.imedea.uib-csic.es/~charles PhD student - Global Change Laboratorio Internacional de Cambio Global Department of Global Change Research Instituto Mediterr?neo de Estudios Avanzados(CSIC/UIB) Calle Miquel Marques 21, 07190 Esporles - Islas Baleares - Espa?a Office phone - +34 971 610 896 Cell phone - +34 660 207 940 ______________________________________________ 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.