How to multiply two multi dimension array in R?

Viewed 62

I have two multi dimension array in R like

c_t <- array(0, dim = c(2,5,4,3,2), 
             dimnames = list(c("id1", "id2"),
                             c("t1","t2","t3","t4","t5"),
                             c("occ1","occ2","occ3","occ4"),
                             c("age1","age2","age3"),
                             c("lvl1","lvl2")))
cc <- array(1:96, dim = c(2,2,4,3,2),
            dimnames = list(c("id1", "id2"),
                            c("id1", "id2"),
                            c("occ1","occ2","occ3","occ4"),
                            c("age1","age2","age3"),
                            c("lvl1","lvl2")))
c_t[,1,,,]=1

Which "c_t" is the situation of people (id) in time t, We assume at time 1 all ids are 1 and "cc" is switching matrix from time t to t+1. I want to multiply this tow so I can find the id situations. I've already tried for loop.

for (t in 2:(dim(c_t)[2])-1) {#t is time dimension
  for (i in 1:dim(c_t)[1]) {#i is id dimension
    for (j in 1:dim(c_t)[3]) {#j is occ dimension
      for (k in 1:dim(c_t)[4]) {#k is age dimension
        for (l in 1:dim(c_t)[5]){#l is lvl dimension
          c_t[,t+1,,,][i,j,k,l]=c_t[,t,j,k,l]%*%cc[,i,j,k,l]
        }
      }
    }
  }
}

Is there any better way (right way) for multiply this tow array?

0 Answers
Related