I wanted to do a permutation test and this is the structure of the code(with dummy data). The permutations will run in parallel and want to count how many times the generated matrix fails the test. (see code block 2). But this is slow in the block 2 because it works in a single processor. I want to write a .combine function to go with the foreach() function but I don't know how to give the input parameters(cc and a matrices)
library(foreach)
library(parallel)
#matrix to be populated
cc<-matrix(0,nrow = 10,ncol = 10)
#fixed matrix
a<-matrix(runif(100), ncol=10)
iters<-1e3
cl<-makeCluster(8)
registerDoParallel(cl)
ls<-foreach(icount(iters)) %dopar% {
#generated matrix
b<-matrix(runif(100), ncol=10)
b
}
stopCluster(cl)
This part is the problem. I want to count how many times each element of matrix b is greater than the fixed matrix a and add the count for each element to cc matrix. If i can define a .combine function this should execute when each matrix is generated.
for(b in ls){
for(i in 1:dim(a)[1]) {
for(j in 1:dim(a)[2]) {
if(a[i,j] < b[i,j]) cc[i,j]=cc[i,j] + 1
}
}
}
cc