我有一个四维数组,其中dim1& dim 2表示高度和宽度,dim3表示我的数据集中的位置。我有第四个维度代表时间因素。
LocA<-c("NewYork","Rio","Tokio")
Time<-c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8")
assign("Mat",floor(array(0,c(100,100,length(LocA),length(Time)),dimnames=list(NULL,NULL,LocA,Time))))
现在我想使用Leslie-Matrix并将dim1-3的每个元素与2d-Leslie矩阵相乘。
我的设置是:
TimeLeslie<-array(0,c(8,8))
TimeLeslie[1,]<-c(0,0.3,0,0,0,0,0,0)
TimeLeslie[2,]<-c(0,0,0.3,0,0,0,0,0)
TimeLeslie[3,]<-c(0,0,0,0.3,0,0,0,0)
TimeLeslie[4,]<-c(0,0,0,0,0.3,0,0,0)
TimeLeslie[5,]<-c(0,0,0,0,0,0.3,0,0)
TimeLeslie[6,]<-c(0,0,0,0,0,0,0.3,0)
TimeLeslie[7,]<-c(0,0,0,0,0,0,0,0.3)
TimeLeslie[8,]<-c(0,0,0,0,0,0,0,0.3)
TimeLeslie
Mat[,,,"Y1"]<-1000
我可以通过使用嵌套for循环来实现我想要的结果:
for(i in 1:100){
for(j in 1:100){
for(loc in LocA){
Mat[i,j,loc,]<- Mat[i,j,loc,]%*%TimeLeslie
}
}
}
但是,使用矢量化是否有更快的解决方案。如果是这样,如何编码呢?
修改 看到问题的复杂性,我通过将数组拆分为三个来减少维数。
for(loc in LocA){
assign(paste("Mat",loc,sep=""),floor(array(0,c(100,100,length(Time)),dimnames=list(NULL,NULL,Time))))
}
我怎么能离开这里?