Re: [R] How to quickly find the position of the first non-zero element of a vector

2009-09-15 Thread jim holtman
Fast fingers. Had to change the test: > vlarge <- c(numeric(20),1:20) > system.time( + for (i in 1:3) { +which.min(!(vlarge != 0))}) # test changed user system elapsed 0.350.000.42 > > #Method 2: > system.time( + for (i in 1:3) { + for (i in 1:40) { + if (vlarge[

Re: [R] How to quickly find the position of the first non-zero element of a vector

2009-09-15 Thread jim holtman
try .which.mine Here is what is does on my system: > vlarge <- c(numeric(20),1:20) > system.time( + for (i in 1:3) { +which.min(vlarge != 0)}) user system elapsed 0.300.000.35 > > #Method 2: > system.time( + for (i in 1:3) { + for (i in 1:40) { + if (vlarge[i] != 0

[R] How to quickly find the position of the first non-zero element of a vector

2009-09-15 Thread Bryan Keller
Anyone familiar with a quicker method to find the position of the first non-zero element of a vector? #The first way: print(min(which(vector != 0))) #The second way: for(i in 1:length(vector)) { if (vector[i] != 0) { print(i) break }