Efficiently perform row-wise distribution test
问题 I have a matrix in which each row is a sample from a distribution. I want to do a rolling comparison of the distributions using ks.test and save the test statistic in each case. The simplest way to implement this conceptually is with a loop: set.seed(1942) mt <- rbind(rnorm(5), rnorm(5), rnorm(5), rnorm(5)) results <- matrix(as.numeric(rep(NA, nrow(mt)))) for (i in 2 : nrow(mt)) { results[i] <- ks.test(x = mt[i - 1, ], y = mt[i, ])$statistic } However, my real data has ~400 columns and ~300