Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Component-wise addition of matrices as list elements

Tags:

r

So I have a 1131 element list, with each element being a 5 by 5 matrix. The first element looks much like the other ones

sotest.corr.try[1]
[[1]]
            [,1]        [,2]       [,3]
[1,]  1.00000000 -0.04125426  0.1565728
[2,] -0.04125426  1.00000000  0.1199373
[3,]  0.15657281  0.11993733  1.0000000
[4,]  0.10209354  0.06125212  0.1937589
[5,] -0.19069820  0.17598585 -0.1235949
            [,4]        [,5]
[1,]  0.10209354 -0.19069820
[2,]  0.06125212  0.17598585
[3,]  0.19375885 -0.12359492
[4,]  1.00000000 -0.08771679
[5,] -0.08771679  1.00000000

Starting at element 126, I'd like to just add the preceding 125 matrices to 126. So that the component in the 1,2 spot, for example, would be the sum of the first 126 1,2 components. I've noticed that something like this gets what I want

sotest.corr.try[[1]]+sotest.corr.try[[2]]
            [,1]        [,2]       [,3]       [,4]
[1,]  2.00000000 -0.08842164  0.3155670  0.2063603
[2,] -0.08842164  2.00000000  0.2363135  0.1156103
[3,]  0.31556697  0.23631345  2.0000000  0.3869373
[4,]  0.20636030  0.11561033  0.3869373  2.0000000
[5,] -0.38288102  0.35103362 -0.2489587 -0.1804376
           [,5]
[1,] -0.3828810
[2,]  0.3510336
[3,] -0.2489587
[4,] -0.1804376
[5,]  2.0000000

But this doesn't

sum(sotest.corr.try[[1:126]])
Error in sotest.corr.try[[1:126]] : recursive indexing failed at level 2

Is there any way to do this quickly? Maybe using lapply? Thanks

like image 424
jlewis Avatar asked Jan 02 '22 19:01

jlewis


People also ask

How do you find the sum of the elements in a matrix?

Description. S = sum( A ) returns the sum of the elements of A along the first array dimension whose size does not equal 1. If A is a vector, then sum(A) returns the sum of the elements. If A is a matrix, then sum(A) returns a row vector containing the sum of each column.

What is an element-wise sum?

Element-wise addition of two lists basically means adding the first element of list1 and the first element of list2 and so on. There are several methods that perform this operation. Every method has its own uniqueness. Some of them work on unequal lengths while some works on lists of equal lengths.

What is element-wise in R?

In R the asterisk (*) is used for element-wise multiplication. This is where the elements in the same row are multiplied by one another. #These will give the same result c*x x*c. We can see that the output of c*x and x*c are the same, and the vector x doubles matrix c.

What is element-wise addition?

element-wise addition is also called matrix addtion, for example: There is an example to show how to calculate element-wise addtion. If the dimension of A and B is different, we may to add each element by row or column. In academic papers, we often use ⊕ symbol to express element-wise addition.

How to add two lists element-wise in C++?

This example uses for loop and append () function to add two lists element-wise. It allows lists of unequal lengths. It finds a smaller list between the two and then iterates over the elements of the shorter list using for loop. append () function returns the sum of two elements.

What are matrices?

Matrices and Linear Algebra Matrices arise in many, many, many different contexts. Most generally a matrix is simply a rectangular array of entities also called the components of the matrix.

How do you do matrix addition with rings and fields?

Of course, standard matrix notions can be generalized and expressed using rings and fields, which is the approach used here. with m rows, n columns, and elements aij from a field F (or alternatively, A ∈ Fm×n). Define matrix addition ( A + B = C) element-wise ( aij + bij = cij) for A, B, and C ∈ Fm×n; i = 1, 2, …, m; and j = 1, 2, …, n.


Video Answer


2 Answers

For purposes of illustration suppose we have a list L of 5 2x2 matrices and we want the output to be the first two, followed by the cumulative sums for the others.

1) We concatenate the first two components of the list with everything but the first two components of the cumulative sum list computed using Reduce.

# test input
M <- matrix(1:4, 2)
L <- list(M, 2*M, 3*M, 4*M, 5*M)

ix <- 1:2
out1 <- c(L[ix], Reduce(`+`, L, acc = TRUE)[-ix])

# check
out2 <- list(L[[1]], L[[2]], L[[1]] + L[[2]] + L[[3]], 
L[[1]] + L[[2]] + L[[3]] + L[[4]], L[[1]] + L[[2]] + L[[3]] + L[[4]] + L[[5]])
identical(out1, out2)
## [1] TRUE

2) A simple for loop would also work. Input L is from (1).

L2 <- L
for(i in seq_along(L2)[-1]) L2[[i]] <- L2[[i]] + L2[[i-1]]
ix <- 1:2
out3 <- c(L[ix], L2[-ix])

# check - out2 is from (1)
identical(out2, out3)
## [1] TRUE
like image 109
G. Grothendieck Avatar answered Oct 19 '22 00:10

G. Grothendieck


Here are two other options using apply or rowSums with array (borrow data from G. Grothendieck's answer)

> apply(
+   array(
+     do.call(
+       cbind,
+       L
+     ), c(2, 2, length(L))
+   ), 1:2, sum
+ )
     [,1] [,2]
[1,]   15   45
[2,]   30   60

> rowSums(
+   array(
+     do.call(
+       cbind,
+       L
+     ), c(2, 2, length(L))
+   ),
+   dims = 2
+ )
     [,1] [,2]
[1,]   15   45
[2,]   30   60
like image 2
ThomasIsCoding Avatar answered Oct 19 '22 00:10

ThomasIsCoding