In R, I have two matrices, x and y, which both have the same number of columns, say, for example:
x <- matrix(runif(10*20),10,20)
y <- matrix(runif(50*20),50,20)
What is the most efficient way to create a matrix which contains the result of the following comparison. Compare each row in x to each row in y (10x50 row comparisons), return how many numbers in the row of y are smaller than the corresponding number in the row of x. Put the results in a 10x50 result matrix.
The following code works, but it is not efficient:
result <- matrix(NA,10,50)
for (i in 1:10) {
for (j in 1:50) {
result[i,j]<- sum(x[i,]>y[j,])
}
}