Hi, Is there a function func(x) where x is an array such that it returns a list of the indices of all non-zero elements of the array? for example: a: 1 0 0 0 0 1 0 1 0 func(a) returns two vectors r and c: r c 1 1 2 3 3 2 daver +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |David Richmond It works on a | + Dept. of Sociology complex scientific + |Saint Mary's College principle, known as | + Notre Dame, IN 46556 "pot luck." + |219-284-4517 - The Doctor | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Is there a function func(x) where x is an array such that it > returns a list of the indices of all non-zero elements of the array? >which() eg.> a<-matrix(c(1,0,0,0,0,1,0,1,0),ncol=3) > a[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 0 1 [3,] 0 1 0> which(a != 0, arr.ind=TRUE)row col [1,] 1 1 [2,] 3 2 [3,] 2 3 That do? Cheers Jason -- Indigo Industrial Controls Ltd. 64-21-343-545 jasont at indigoindustrial.co.nz -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, use: which( a>0, arr.ind=TRUE) best wishes, Ott On Mon, 8 Oct 2001, David A Richmond wrote:> Hi, > Is there a function func(x) where x is an array such that it > returns a list of the indices of all non-zero elements of the array? > > for example: > a: > 1 0 0 > 0 0 1 > 0 1 0 > > func(a) returns two vectors r and c: > r c > 1 1 > 2 3 > 3 2 > > > daver-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> x <- cbind(c(1,0,0),c(0,0,1),c(0,1,0)) > x[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 0 1 [3,] 0 1 0> cbind(row(x)[x!=0],col(x)[x!=0])[,1] [,2] [1,] 1 1 [2,] 3 2 [3,] 2 3 Agus Dr. Agustin Lobo Instituto de Ciencias de la Tierra (CSIC) Lluis Sole Sabaris s/n 08028 Barcelona SPAIN tel 34 93409 5410 fax 34 93411 0012 alobo at ija.csic.es On Mon, 8 Oct 2001, David A Richmond wrote:> Hi, > Is there a function func(x) where x is an array such that it > returns a list of the indices of all non-zero elements of the array? > > for example: > a: > 1 0 0 > 0 0 1 > 0 1 0 > > func(a) returns two vectors r and c: > r c > 1 1 > 2 3 > 3 2 > > > daver > > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > |David Richmond It works on a | > + Dept. of Sociology complex scientific + > |Saint Mary's College principle, known as | > + Notre Dame, IN 46556 "pot luck." + > |219-284-4517 - The Doctor | > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._