0
votes

Suppose I have a list of two matrices and

t=c(1,2,3,4).  

 > y_list
    [[1]]
          [,1] [,2]
     [1,]    1   11
     [2,]    2   12
     [3,]    3   13
     [4,]    4   14
     [5,]    5   15
     [6,]    6   16
     [7,]    7   17
     [8,]    8   18
     [9,]    9   19
    [10,]   10   20

    [[2]]
          [,1] [,2]
     [1,]   21   31
     [2,]   22   32
     [3,]   23   33
     [4,]   24   34
     [5,]   25   35
     [6,]   26   36
     [7,]   27   37
     [8,]   28   38
     [9,]   29   39
    [10,]   30   40

I want to do another list of two matrices of order 10 by 4(length of t). I can do it for individual matrix. For the first matrix

n.iter=nrow(y_list[[1]])
t.i=c(01,2,3,4)
y_list.1=matrix(NA, nrow = n.iter, ncol=length(t.i))
for( iter in 1:n.iter){
  for (t in 1:length(t.i)){
    y_list.1[iter,t]=y_list[[1]][iter,1]+y_list[[1]][iter,2]*t.i[t]
  }
}
y_list.1

> y_list.1
      [,1] [,2] [,3] [,4]
 [1,]   12   23   34   45
 [2,]   14   26   38   50
 [3,]   16   29   42   55
 [4,]   18   32   46   60
 [5,]   20   35   50   65
 [6,]   22   38   54   70
 [7,]   24   41   58   75
 [8,]   26   44   62   80
 [9,]   28   47   66   85
[10,]   30   50   70   90

I want to the same task for the second matrix in the list y_list. How can I do another list of two matrices of order 10 by 4 using for loop? Thanks in advance

1
Please (a) post a (copy/pasteable!) reproducible sample of data. Simulated data like set.seed(123); M = replicate(n = 5, matrix(rnorm(6), nrow = 3)) is fine, or if you already have a suitable M give us dput(M) which will be copy-pasteable. Edit it in your question, not a comment. (b) Show the code you use to do it to one matrix. That will help explain what you want to do. I'm confused by that you start with 5 matrices, they each have 2 columns, and somehow you use 4 t values. How do the 4 t values map to the 5 matrices?Gregor Thomas
Is t[1] only used for M[[1]]? (If so, what do we do for M[[5]], use t[1] again?) Or do you want every combination of t and M, for 20 matrices in the result? Or something else?Gregor Thomas
Hello Gregor, Thanks for your prompt reply. For each t value will be used for each matrix. like for the first matrix, the first entry i,e(1,1) should be M[[1]][1,1]+M[[1]][1,2]*t[1], second entry of the first row i,e(1,2) should be M[[1]][1,1]+M[[1]][1,2]*t[2] , second row first entry should be M[[1]][2,1]+M[[1]][2,2]*t[2]. This will continue for all matrices. hence Finally I will have a list of 5 matrices and each will have dimension of 3 by 4.Uddin
I think a full worked example would be nice---I still don't understand. When you edit your question with the reproducible example, please show the expected result for the first matrix in M.Gregor Thomas
Hi Gregor, I have edited the question. Can you please check and let me know if it can be done. ThanksUddin

1 Answers

2
votes

First reduce your calculation to:

y_list <- list(matrix(1:20, 10), matrix(21:40, 10))

t.i <- c(1, 2, 3, 4)
y_list.1 <- y_list[[1]][,1] + tcrossprod(y_list[[1]][,2], t.i)
y_list.1

Now it is clear that you can do for your list of matrices:

lapply(y_list, function(y) y[,1] + tcrossprod(y[,2], t.i))

The result is a list of the new calculated matrices.