Skip to content

[External] Re: Speeding up a loop

2 messages · Rui Barradas

#
Hello,

Are you sure? With a matirx composed of those two rows only I had a 
problem, the function to.keep() returned NULL. See the changes made to 
avoid it.

# beginning of loop
     for(i in seq_len(nrow(x))){
         #yes <- x[i, 1] > a1 | x[i, 2] > a2 | x[i, 3] < a3 | x[i, 4] > a4
         #if(all(yes)) keep(i, e)
         # Original post, do NOT remove if equal
         #no <- x[i, 1] < a1 | x[i, 2] < a2 | x[i, 3] > a3 | x[i, 4] < a4
         # Changed to remove if equal
         no <- x[i, 1] <= a1 | x[i, 2] <= a2 | x[i, 3] >= a3 | x[i, 4] <= a4
         if(all(!no)) keep(i, e)
     }
     if(e$ires == 0 && nrow(x) > 0)
         x[1, ]
     else
         e$result[seq_len(e$ires), 1:nc]
# end of function


Em 23-07-2012 18:18, Reith, William [USA] escreveu:
#
Hello,

Sorry for the mess, the logical operation should also be changed to 
conjunction:

         no <- x[i, 1] <= a1 & x[i, 2] <= a2 & x[i, 3] >= a3 & x[i, 4] <= a4

Rui Barradas

Em 24-07-2012 13:09, Rui Barradas escreveu: