user7905871
user7905871

Reputation:

multiply list of matrices by the list of arrays where they are from different length in R

I would like to multiply matrices by arrays where they have different lengths.

Note: the multiplication should be element by the matches element.

1- I have a list of two matrices.

2- I have a list of arrays (three arrays for each element of the list).

I would like to do the following:

3- Multiply the first matrix by the 3 arrays of the first element of the arrays list.

4- Multiply the second matrix by the 3 arrays of the second element of the arrays list.

Then the expected output would be:

1- 3 arrays from the multiplication of the first matrix by the 3 arrays.

2- 3 arrays from the second multiplication.

However, I only got 3 arrays.

I can do that manually but I would like to do it automatically.

Here is the code:

set.seed(47)
a <- vector('list', 2)
for( i in seq_along(a)){       
  a[[i]] <- array(rnorm(5 * 5 * 3), c(5, 5, 3))
}

we1 <- c(0, 0.2, 0.5, 0.5, 0.9,
         0, 0, 0.1, 0.6, 0.9,
         0, 0, 0, 0.9, 0.5,
         0, 0, 0, 0, 0.8,
         0, 0, 0, 0, 0)
we1 <- matrix(we1, 5, 5)
we2 <- c(0, 0.2, 0.5, 0.5, 0.9,
         0, 0, 0.1, 0.6, 0.9,
         0, 0, 0, 0.9, 0.5,
         0, 0, 0, 0, 0.8,
         0, 0, 0, 0, 0)
we2 <- matrix(we2, 5, 5)
we <- list(we1, we2)

x <- vector("list",2)
for(j in 1:2){
for( i in 1:3){
  x[[i]] <-  we[[j]]*a[[j]][[i]]
}
}

The output:

> x
[[1]]
           [,1]        [,2]       [,3]       [,4] [,5]
[1,]  0.0000000  0.00000000  0.0000000  0.0000000    0
[2,] -0.1878045  0.00000000  0.0000000  0.0000000    0
[3,] -0.4695112 -0.09390224  0.0000000  0.0000000    0
[4,] -0.4695112 -0.56341343 -0.8451202  0.0000000    0
[5,] -0.8451202 -0.84512015 -0.4695112 -0.7512179    0

[[2]]
          [,1]       [,2]      [,3]      [,4] [,5]
[1,] 0.0000000 0.00000000 0.0000000 0.0000000    0
[2,] 0.1676886 0.00000000 0.0000000 0.0000000    0
[3,] 0.4192215 0.08384429 0.0000000 0.0000000    0
[4,] 0.4192215 0.50306575 0.7545986 0.0000000    0
[5,] 0.7545986 0.75459863 0.4192215 0.6707543    0

[[3]]
          [,1]       [,2]      [,3]      [,4] [,5]
[1,] 0.0000000 0.00000000 0.0000000 0.0000000    0
[2,] 0.1058393 0.00000000 0.0000000 0.0000000    0
[3,] 0.2645984 0.05291967 0.0000000 0.0000000    0
[4,] 0.2645984 0.31751803 0.4762770 0.0000000    0
[5,] 0.4762770 0.47627705 0.2645984 0.4233574    0

Any help, please?

Upvotes: 1

Views: 43

Answers (1)

akrun
akrun

Reputation: 887048

We can either use lapply or Map. As the number of list elements are the same for 'a' and 'we', but only only one of them is array i.e. 'a', we can replicate the 'we' by the third dimension of a[[i]]

Map(function(x, y) x*replicate(dim(x)[3], y), a, we)

Or loop through the sequence of list elements and do the multiplication after replication as mentioned above

lapply(seq_along(we), function(i) a[[i]]*array(we[[i]], dim(a[[i]])))

Here, we use array, which will do the recycling of the elements and subsequent replication before multiplying.


If we are using for loop

for(i in seq_along(x)) x[[i]] <- replicate(dim(a[[i]])[3], we[[i]]) * a[[i]]

Upvotes: 1

Related Questions