[R] find indices of nonzero elements

Jason Turner jasont at indigoindustrial.co.nz
Tue Oct 9 07:37:29 CEST 2001


> 	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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list