Sum a list of matrices
Use Reduce
.
## dummy data
.list <- list(matrix(1:25, ncol = 5), matrix(1:25, ncol = 5))
Reduce('+', .list)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 12 22 32 42
## [2,] 4 14 24 34 44
## [3,] 6 16 26 36 46
## [4,] 8 18 28 38 48
## [5,] 10 20 30 40 50
Sum of a list of matrices in R
Since you want to keep top-level list use lapply
:
lapply(x, function(l) if(is.list(l)) Reduce(`+`, l) else l)
#[[1]]
# [,1] [,2] [,3]
#[1,] 3 9 15
#[2,] 5 11 17
#[3,] 7 13 19
#[[2]]
# [,1] [,2] [,3]
#[1,] 3 6 9
#[2,] 4 7 10
#[3,] 5 8 11
#[[3]]
# [,1] [,2] [,3]
#[1,] 4 7 10
#[2,] 5 8 11
#[3,] 6 9 12
How to sum all the arrays inside a list of arrays?
Just sum
the list:
>>> sum([np.array([[5,0,0],[0,5,0],[0,0,5]]), np.array([[1,1,0],[2,4,0],[2,0,5]])])
array([[ 6, 1, 0],
[ 2, 9, 0],
[ 2, 0, 10]])
R: how do apply the sum function in a list?
You need to find someway to group your columns by threes, for example:
grp = (1:ncol(Matrix) -1) %/% 3
or if you know the dimensions:
grp = rep(0:2,each=3)
To do rowSums in columns of threes, we can do this with a function:
SumCols = function(M,col_grp){
sapply(unique(col_grp),function(i)rowSums(M[,col_grp==i]))
}
SumCols(Matrix,grp)
[,1] [,2] [,3]
[1,] 18 63 108
[2,] 21 66 111
[3,] 24 69 114
[4,] 27 72 117
[5,] 30 75 120
So put this inside your List of matrices,
Reduce("+",lapply(List[[1]],SumCols,grp))
[,1] [,2] [,3]
[1,] 540 1890 3240
[2,] 630 1980 3330
[3,] 720 2070 3420
[4,] 810 2160 3510
[5,] 900 2250 3600
Sum list of matrices with NAs
We can write a custom function and use it in Reduce
. We replace the NA
s with 0's and then we add them.
modifiedSum <- function(x, y) {
replace(x, is.na(x), 0) + replace(y, is.na(y), 0)
}
Reduce(modifiedSum, lx)
# [,1] [,2]
#[1,] 1 5
#[2,] 5 6
Rolling sum of matrices in list
library(purrr)
map(split(test_list, ceiling(seq_along(test_list)/3)), ~reduce(.x , `+`))
$`1`
[,1] [,2] [,3]
[1,] 11 14 17
[2,] 12 15 18
[3,] 13 16 19
$`2`
[,1] [,2] [,3]
[1,] 3 12 21
[2,] 6 15 24
[3,] 9 18 27
Credit to this answer for the neat splitting code.
Make numpy.sum() return a sum of matrices instead of a single number
You must call np.sum with the optional axis parameter set to 0 (summation over the axis 0, i.e the one created by your list comprehension)
totalsum = np.sum([i * matrix for i in arr], 0)
Alternatively, you can omit the brackets so np.sum evaluate a generator.
totalsum = np.sum(i * matrix for i in arr)
Sum of elements of a matrix (list), but only at indexes given in another matrix (list)
Use a generator expression:
res = sum(v for ii, val in zip(index, matrix) for i, v in zip(ii, val) if i == -1)
print(res)
Output
935
As an alternative:
res = 0
for ii, val in zip(index, matrix):
for i, v in zip(ii, val):
if i == -1:
res += v
Related Topics
R Data.Table Apply Function to Rows Using Columns as Arguments
Difference Between R-Base and R-Recommended Packages
How to Use Dplyr's Summarize and Which() to Lookup Min/Max Values
Group Data and Plot Multiple Lines
How to Embed an Image in a Cell a Table Using Dt, R and Shiny
Ggplot Scale Color Gradient to Range Outside of Data Range
Knitr Wont Compile PDF: "Error in Tools::File_Path_As_Absolute(Output_File)"
How to Group by Two Columns in R
Add Image in Title Page of Rmarkdown PDF
Add Values to a Reactive Table in Shiny
Creating (And Accessing) a Sparse Matrix with Na Default Entries
How to Strip Dollar Signs ($) from Data/ Escape Special Characters in R
Dealing with Very Small Numbers in R
Unicode with Knitr and Rmarkdown
R Solve:System Is Exactly Singular
How to Use R to Download a Zipped File from a Ssl Page That Requires Cookies