Sue
Sue

Reputation: 189

In R, faster way than for loop or apply

For these two matrices, I want to find the product of the matrix X and the row of Q and apply ifelse function to see if the product is greater than zero.

n1=1000, m=10000
X=cbind(rnorm(n1),rbinom(n1))
Q=matrix(rnorm(2*m), ncol=2)

To do this, I tried for loop and apply function in the following.

D=10000
ind_beta=matrix(0,n1,D)
for (l in 1:D){
ind[,l]=as.vector(ifelse(X%*%Q[l,]>=0,1,0))   
}

and

ind=apply(Q,1,function(x){ifelse(X%*%Q>=0,1,0)})

Both codes give the same result, but it is really time consuming. Is there any way to make this fast? Thanks in advance.

Upvotes: 1

Views: 895

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226087

How about:

Make data (reproducibly):

set.seed(101)
n1=1000; m=10000
X=cbind(rnorm(n1),rbinom(n1,size=1,prob=0.6))
Q=matrix(rnorm(2*m), ncol=2)

Your way takes about 2.5 seconds:

system.time(ind <- apply(Q,1,function(x){ifelse(X%*%x>=0,1,0)}))

This takes about 0.3 seconds:

system.time({
    XQ <- X %*% t(Q)
    ind2 <- matrix(as.numeric(XQ>=0),nrow(XQ))
})

Results match:

all.equal(ind,ind2) ## TRUE

Upvotes: 5

Related Questions