[R] Help on improving an algorithm

li li hannah.hlx at gmail.com
Wed Jul 27 22:41:46 CEST 2016


Hi all,
 I have four matrix tmp_a, tmp_b, tmp_c and tmp_d whose dimensions are
shown as below.
I want to take one row from each of these matrices and then put the four
selected rows into a matrix.
I want to count the number of such matrices for which the vector of row sum
is less than or equal to (8,8,8,8,8) (componentwise).
Below is the code I use now. Is there a way to make this more efficient and
faster?
 Thanks for the help in advance.

> dim(tmp_a);dim(tmp_b); dim(tmp_c); dim(tmp_d)[1] 252   6
[1] 126   6
[1] 126   6
[1] 126   6


p <- 0
for (i in 1:dim(tmp_a)[1]){
    for (j in 1:dim(tmp_b)[1]){
        for (k in 1:dim(tmp_c)[1]){
            for (l in 1:dim(tmp_c)[1]){
            conti <- rbind(tmp_a[i,], tmp_b[j,], tmp_c[k,],tmp_d[l,])
            if (sum(apply(conti,1,sum)>8)==0)
                p <- p+1
                print(p)}}}}

	[[alternative HTML version deleted]]



More information about the R-help mailing list