Hello All, Is there a fast way to find the index(row and column) of a point in a matrix? Thanks, John. -- -------------------------------------------------------------------------- Dr. John Janmaat Department of Economics, Acadia University, Wolfville, NS, B4P 2R6 E-mail: jjanmaat at acadiau.ca Web: http://ace.acadiau.ca/~jjanmaat Tel: 902-585-1461 Fax: 902-585-1070
Try these: which.col <- function (mat, x) (which(mat==x)-1) %/% nrow(mat) + 1 which.row <- function (mat, x) (which(mat==x)-1) %% nrow(mat) + 1 Knowing the R community, there may be already functions to do this.> Is there a fast way to find the index(row and column) of a > point in a matrix?
>-----Original Message----- >From: r-help-bounces at stat.math.ethz.ch >[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of John Janmaat >Sent: Wednesday, April 02, 2003 12:39 PM >To: r-help at stat.math.ethz.ch >Subject: [R] Index of item in matrix > > >Hello All, > >Is there a fast way to find the index(row and column) of a point in a>matrix? > >Thanks, > >John.Look at ?which. Specifically the form: which(x, arr.ind = TRUE)> m <- matrix(1:12,3,4) > m[,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12> which(m == 9, arr.ind = TRUE)row col [1,] 3 3> m[3,3][1] 9 HTH, Marc Schwartz
Satisfying what conditions? Does the following example help you get what you want? > set.seed(1) > A <- array(rnorm(12), dim=c(3,4)) > > sel <- (round(A)==1) > sel [,1] [,2] [,3] [,4] [1,] FALSE FALSE FALSE TRUE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE FALSE FALSE > cbind(row(A)[sel],col(A)[sel]) [,1] [,2] [1,] 2 2 [2,] 1 4 Spencer Graves John Janmaat wrote:> Hello All, > > Is there a fast way to find the index(row and column) of a point in a > matrix? > > Thanks, > > John.
Hi, | Date: Wed, 02 Apr 2003 14:39:05 -0400 | From: John Janmaat <john.janmaat at acadiau.ca> | Hello All, | | Is there a fast way to find the index(row and column) of a point in a | matrix? what do you mean as ,,a point in a matrix''? Perhaps which( ,arr.ind=T) does what you are looking for. Best wishes, Ott
On Wed, 2 Apr 2003, John Janmaat wrote:> Hello All, > > Is there a fast way to find the index(row and column) of a point in a > matrix?> x <- matrix(rnorm(25), 5, 5) > which(x < -1, arr.ind=TRUE)row col [1,] 1 1 [2,] 2 1 [3,] 2 2 [4,] 5 2 [5,] 1 3 [6,] 2 4 -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
> Hello All, > > Is there a fast way to find the index(row and column) of a point in a > matrix? >R> a <- matrix(1:25, ncol=5) R> which(a == 2, arr.ind=TRUE) row col [1,] 2 1 R> a [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 best, Torsten> Thanks, > > John. > -- > -------------------------------------------------------------------------- > Dr. John Janmaat > Department of Economics, Acadia University, Wolfville, NS, B4P 2R6 > E-mail: jjanmaat at acadiau.ca Web: http://ace.acadiau.ca/~jjanmaat > Tel: 902-585-1461 Fax: 902-585-1070 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
Or more simply: > set.seed(1) > A <- array(rnorm(12), dim=c(3,4)) > which(round(A) == 1, arr.ind = TRUE) row col [1,] 2 2 [2,] 1 4 Though, the original post may need more clarification. Sundar Spencer Graves wrote:> Satisfying what conditions? > > Does the following example help you get what you want? > > > set.seed(1) > > A <- array(rnorm(12), dim=c(3,4)) > > > > sel <- (round(A)==1) > > sel > [,1] [,2] [,3] [,4] > [1,] FALSE FALSE FALSE TRUE > [2,] FALSE TRUE FALSE FALSE > [3,] FALSE FALSE FALSE FALSE > > cbind(row(A)[sel],col(A)[sel]) > [,1] [,2] > [1,] 2 2 > [2,] 1 4 > > Spencer Graves > > John Janmaat wrote: > >> Hello All, >> >> Is there a fast way to find the index(row and column) of a point in a >> matrix? >> >> Thanks, >> >> John. > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >