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[
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
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
}
3 matches
Mail list logo