# RE: [R] How to search an element in matrix ?

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Mon 11 Apr 2005 - 19:23:55 EST

Actually, you will need to use arr.ind=TRUE which is not the default option. You will get the results in form [i, j] indicating the i^{th} row and j^{th} column of the element that passes the criteria.

m <- matrix( rnorm(6), nc=3 )
m

[,1] [,2] [,3]
[1,] 0.5066194 0.786876 -1.2848658
[2,] -0.2018563 2.007892 0.4581891

which( m > 0.5, arr.ind=T )

row col

```[1,]   1   1
[2,]   1   2
[3,]   2   2

```

On Sun, 2005-04-10 at 21:51 -0700, Vadim Ogranovich wrote:
> A matrix is a vector as well (it is stored by columns), so it has two
> ways of indexing [i,j] and [i]. It may be easier for you to use the
> latter, thus
> which(x == 1) returns all indexes where the matrix x is equal to 1.
>
> > -----Original Message-----
> > From: r-help-bounces@stat.math.ethz.ch
> > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of tong wang
> > Sent: Sunday, April 10, 2005 9:37 PM
> > To: r-help@stat.math.ethz.ch
> > Subject: [R] How to search an element in matrix ?
> >
> > Hi you guys,
> > I know this might be too simple a question to post, but
> > i searched a lot still couldn't find it.
> > Just want to find an element in matrix and return its
> > index , i think there should be some matrix version of
> > "match" which only works for vector to me.
> > thanks in advance for your help.
> >
> > best,
> > tong
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Apr 11 19:28:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:06 EST