在R中的子矩阵中存储输出

时间:2018-05-12 15:17:37

标签: r matrix

Nh<-matrix(c(17,26,30,17,23, 17 ,24, 23), nrow=2, ncol=4h); Nh
Sh<-matrix(c(8.290133, 6.241174, 6.096808, 7.4449672, 6.894924, 7.692115, 
4.540521, 7.409122), nrow=2, ncol=4); Sh
NhSh<-as.matrix(Nh*Sh); NhSh
rh<-c( 0.70710678, 0.40824829, 0.28867513, 0.22360680, 0.18257419, 
0.15430335, 0.13363062, 0.11785113, 0.10540926, 0.09534626); rh

pv<-matrix(nrow=4, ncol=10)
for (i in 1:4) 
    {
      pv[i,]<-as.matrix(NhSh[1,i]*rh) 
    }
pv

对于1行,它可以很好地给出输出

 #> pv
    ########[,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
    #[1,]  99.65416 57.53535 40.68364 31.51341 25.73059 21.74632 18.83287 16.60903 14.85557 13.43736
    #[2,] 129.33283 74.67034 52.79991 40.89863 33.39359 28.22274 24.44161 21.55547 19.27980 17.43924
    #[3,] 112.13529 64.74134 45.77904 35.46029 28.95321 24.46993 21.19158 18.68922 16.71614 15.12032
    #[4,]  77.05520 44.48784 31.45765 24.36699 19.89557 16.81482 14.56206 12.84253 11.48671 10.39012

对于两行

pv<-matrix(nrow=8, ncol=10)
for(k in 1:8)
{
for (j in 1:2) # there are 2 rows in NhSh
{
for (i in 1:4) # there are 4 elements in 1 row
{
  pv[k,]<-as.matrix(NhSh[j,i]*rh) # It should multiply 1st element of 1st 
                #row with all values of rh and save results in 1st row of pv 
}
}
}
pv
##> pv
#         [,1]     [,2]     [,3]     [,4]     [,5]    [,6]     [,7]     [,8]     #[,9]    [,10]
#[1,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[2,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[3,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[4,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[5,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[6,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[7,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794
#[8,] 120.4979 69.56951 49.19307 38.10479 31.11243 26.2948 22.77197 20.08299 #17.96277 16.24794

但如果有人可以提供帮助我需要此输出

########[,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
#[1,]  99.65416 57.53535 40.68364 31.51341 25.73059 21.74632 18.83287 16.60903 14.85557 13.43736
#[2,] 129.33283 74.67034 52.79991 40.89863 33.39359 28.22274 24.44161 21.55547 19.27980 17.43924
#[3,] 112.13529 64.74134 45.77904 35.46029 28.95321 24.46993 21.19158 18.68922 16.71614 15.12032
#[4,]  77.05520 44.48784 31.45765 24.36699 19.89557 16.81482 14.56206 12.84253 11.48671 10.39012
#[5,] 114.74259 66.24666 46.84346 36.28479 29.62641 25.03889 21.68431 19.12376 17.10482 15.47189
#[6,]  89.49458 51.66972 36.53601 28.30067 23.10740 19.52932 16.91288 14.91576 13.34106 12.06745
#[7,]  92.46549 53.38498 37.74888 29.24016 23.87449 20.17762 17.47434 15.41092 13.78394 12.46804
#[8,] 120.49793 69.56951 49.19307 38.10479 31.11243 26.29480 22.77197 20.08299 17.96277 16.24794

其次,是否可以给前4行和后4行一个名称,以便我可以调用两个子矩阵,每个子矩阵包含4行和10列,并提供进一步计算的参考?

1 个答案:

答案 0 :(得分:0)

我们可以做到

pv <- c()
for (j in 1:2) {
  for (i in 1:4) {

    pv <- rbind(pv, NhSh[j,i]*rh)
   }
 }


pv
#          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
#[1,]  99.65416 57.53535 40.68364 31.51341 25.73059 21.74632 18.83287 16.60903 14.85557 13.43736
#[2,] 129.33283 74.67034 52.79991 40.89863 33.39359 28.22274 24.44161 21.55547 19.27980 17.43924
#[3,] 112.13529 64.74134 45.77904 35.46029 28.95321 24.46993 21.19158 18.68922 16.71614 15.12032
#[4,]  77.05520 44.48784 31.45765 24.36699 19.89557 16.81482 14.56206 12.84253 11.48671 10.39012
#[5,] 114.74259 66.24666 46.84346 36.28479 29.62641 25.03889 21.68431 19.12376 17.10482 15.47189
#[6,]  89.49458 51.66972 36.53601 28.30067 23.10740 19.52932 16.91288 14.91576 13.34106 12.06745
#[7,]  92.46549 53.38498 37.74888 29.24016 23.87449 20.17762 17.47434 15.41092 13.78394 12.46804
#[8,] 120.49793 69.56951 49.19307 38.10479 31.11243 26.29480 22.77197 20.08299 17.96277 16.24794


row.names(pv) <- rep(c("a", "b"), each = 4)
lapply(split(seq_len(nrow(pv)), row.names(pv)), function(i) pv[i,])