0

I have a list of 1800 matrices that have dimensions 7 x 7. I'm trying to obtain the elements of the first 2 rows and columns and turn it into a list of 2 x 2 matrices before doing a Map multiplication on them to finally replace the values on the original list of 7 x 7 matrices.

The code I have is:

     PreMultiply_matrixList <- list()
     PreMultiply_matrixList <- sapply(Probability_matrixList, "[", c(1:2),c(1:2))

Where the Probability_matrixList is the list of 7 x 7 matrices.

However, the PreMultiply_matrixList just seems to have copied the first element of each matrix, rather than a 2x2 matrix.

I have also tried similar codes from different posts such as:

PreMultiply_matrixList <- sapply(Probability_matrixList,"[",1:2,1:2)

How would you go about it? Then after multiplying (using Map) with another list and then replacing the values on the original Probability_matrixList?

Thanks in advance.

J.Rangel
  • 3
  • 1
  • 2
    Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – lmo Jul 27 '17 at 18:15
  • 1
    Your code is okay (by the way `c(1:2)` and `1:2` are the same) but I think you don't want `sapply` (which simplify the list in a matrix or an array), just use `lapply` instead. – F. Privé Jul 27 '17 at 18:18

1 Answers1

1

for the first step, as per comments above:

PreMultiply_matrixList <- lapply(Probability_matrixList,"[",1:2,1:2)

the second step, using map is up to you.

for the third step, putting the new 2x2 matrices (we'll call them PostMultiply_matrixList ) into the original matrix list, this might work:

Probability_matrixList2 <- lapply(1:length(Probability_matrixList),
 function(x) {
   Probability_matrixList[[x]][1:2,1:2] <- PostMultiply_matrixList[[x]]
   Probability_matrixList[[x]]
  })
Alex P
  • 1,574
  • 13
  • 28