next for loop question.

I need a loop that removes a row from a matrix if it is worse in positions
1,2,3,4 than another row in the matrix. right now my matrix is 503028x26.

Rule to define worse position1 is smaller, position2 is smaller, position3
is higher, and position4 is smaller

Example: 

row1: 1, 10, 3, 3
row2: 3, 7, 5, 2


row2 is not worse than row1 since it is "better" in position 1, eventhough
it is worse in all other positions.

row3: 2,5,7,1
row3 however is worse than row2 and should be removed from the matrix.

Any ideas? Should I break this into pieces or do it all at once? Is there
something faster than a loop? My current loops takes well over 24 hours to
run.


m<-matrix(0,1,24)
for(i in 1:n)
{
 a<-matrix(x[i,1:4],1,4)
j=1
      nn<-nrow(m)
      counter<-0
      while(j<=nn)
      {
        if(a[1]>m[j,1] && a[2]>m[j,2] && a[3]>m[j,4] && a[4]<m[j,4])
        {
          m&lt;-m[-j,]
          nn&lt;-length(m[,1])
          counter&lt;-1
        } else j&lt;-j+1
      }
      if(counter==1)
      {
        b&lt;-cbind(a,x)
         m&lt;-rbind(m,b)
      }
      if(counter==0)
      {
        if(a[1]>min(m[,1]) || a[3]>min(m[,3]) || a[4]>min(m[,4]) ||
a[5]<max(m[,5]))
        {
          b<-cbind(a,x)
           m<-rbind(m,b)
        }
     }
}



--
View this message in context: 
http://r.789695.n4.nabble.com/Speeding-up-a-loop-tp4637201p4637305.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to