multiply each matrix slice in a 3d array by a sing

2019-08-15 20:43发布

Suppose I have a 1 x matrix mat=matrix(1,1,13)

I also have an array that is 13 x 1000 x 10.

dfarray = array(1:(13*1000*10),dim=c(13,1000,10))

Without looping, I want to return the results of this loop

dfarray2=array(NA,dim=c(1,1000,10))

for(i in 1:10){

dfarray2[,,i]=mat%*%dfarray[,,i]


}

1条回答
We Are One
2楼-- · 2019-08-15 21:35

One possibility: deform the dfarray to usual matrix, multiply and transform back to 3d array.

mat <- matrix(1, 1, 13)
dim(dfarray) <- c(13, 1000*10)
dfarray1 <- mat %*% dfarray
dim(dfarray1) <- c(1, 1000, 10)
all(dfarray1==dfarray2)
[1] TRUE
查看更多
登录 后发表回答