This works to multiply the ith row of a by the ith value of b. It might be what you can use
a <- matrix(1:30, 6, 5) b <- 1:6 a a*b To simplify your code, I think you can do this---one multiplication xA <- x %*% A Now you can do the tests on xA and not have any matrix multiplications inside the loop. The next line is not tested but I hope gives you the idea remove.set <- (xA[,1] < .5) || (xA[,2] < 52) || (xA[,3] > 150) new.x <- x[remove.set,] You now have new.x with no explicit loops at all, because this takes advantage of vector operations. Rich On Fri, Jul 20, 2012 at 2:34 PM, wwreith <reith_will...@bah.com> wrote: > That is faster than what I was doing and reducing 15% of my iterations it > still very helpful. > > Next question. > > I need to multiply each row x[i,] of the matrix x by another matrix A. > Specifically > > for(i in 1:n) > { > If (x[i,]%*%A[,1]<.5 || x[i,]%*%A[,2]<42 || x[i,]%*%A[,3]>150) > { > x<-x[-i,] > n<-n-1 > }. #In other words remove row i from x if it does not meet criteria (>=.5, > >=42, <=150). When multiplied to A > } > Is there a better way than using a for loop for this or x<-x[-i,] for that > matter? I assume building a new matrix would be worse. > > Ideally I want to also exclude some x[,i] as well example if x[1,] is > better > than x[2,] in all three categories i.e. bigger, bigger, and smaller than > x[2,] when multiplied to A then I want to exclude x[2,] as well. Any > suggestions on whether it is better to do this all at once or in stages? > > Thanks for helping! > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Speeding-up-a-loop-tp4637201p4637255.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. > [[alternative HTML version deleted]] ______________________________________________ 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.