1
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

For 1 row it works fine giving the output

 #> 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

For both rows

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

but I need this output if anyone can help

########[,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

Secondly, Is it possible to give first 4 rows and last 4 rows a name so that I can call the two sub matrices, each containing 4 rows and 10 columns, with their reference for further calculations?

Sam
  • 95
  • 10

1 Answers1

0

We can do

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,])
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, it worked perfectly fine. There is one more problem, when i repeat 'j' loop for 100 rows and write 'row.names(pv) <- rep(c(1:100), each = 4) lapply(split(seq_len(nrow(pv)), row.names(pv)), function(i) pv[i,])' then it does not generate regular sequence from 1:100 for sub matrices. What is the reason for this? @akrun – Sam May 14 '18 at 04:02
  • @Sam I tried the code, and it is giving me 100 submatrices and the rownames for the first submatrix is 1 and last is 100. Have you changed the 'Nh' and "Sh', nrow to 100? – akrun May 14 '18 at 04:07
  • @akrun- yes, i changed dimension of Nh and Sh to 100*4. Iapply command returns 100 submatrices and rowname for the first submatrix is 1 and last one is 99. it is changing the sequence after every 9th sub matrix. – Sam May 14 '18 at 04:15
  • @Sam Okay, now, I got it what you meant. Is it because the rownames are characters. Suppose, you convert it `numeric`, it will be in the correct order, i.e. `split(seq_len(nrow(pv)), as.numeric(row.names(pv)))` – akrun May 14 '18 at 04:17
  • 1
    @akrun- It's fine now. Thanks a lot for the help – Sam May 14 '18 at 04:19
  • @ akrun- I got another question. lapply returns sub matrices with data type " list" but I need them as "matrix". I changed the datatype of 1 submatrix by using the commands ‘sub<-lapply(split(seq_len(nrow(pv)),as.numeric(row.names(pv))), function(i) pv[i,])…mat<-matrix(unlist(sub[1]), ncol = 10, byrow = FALSE)’ but I am unable to do this for all 100 sub matrices simultaneously. – Sam May 14 '18 at 05:43
  • @Sam it is a `list` of matrices. i.e. `lst <- lapply(split(seq_len(nrow(pv)), row.names(pv)), function(i) pv[i,]); is.matrix(lst[[1]])# [1] TRUE` – akrun May 14 '18 at 05:46
  • You are right but this code gives error if I don't change the class of lst[1] by unlisting it. 'data<-40; b1<-lst[1]; nlargest <- function(b1, data) { res <- order(b1)[seq_len(data)]; pos <- arrayInd(res, dim(b1), useNames = TRUE); list(values = b1[res], position = pos) } nlargest(b1,40);' – Sam May 14 '18 at 05:57
  • @Sam Could you post as a new question as it is not clear to me – akrun May 14 '18 at 05:58
  • https://stackoverflow.com/questions/50324987/manipulating-sub-matrices-in-r; I posted this problem as a new question @akrun – Sam May 14 '18 at 07:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171094/discussion-between-sam-and-akrun). – Sam May 15 '18 at 15:09