Element-wise mean over list of matrices
You can use:
Reduce("+", my.list) / length(my.list)
According to comments, you want both mean
and sd
implemented on a list of matrices, and the above ways will not work smoothly for sd
. Try this instead :
apply(simplify2array(my.list), 1:2, mean)
apply(simplify2array(my.list), 1:2, sd)
Mean of each element of a list of matrices
Maybe what you want is:
> set.seed(1)
> a<-matrix(runif(4))
> b<-matrix(runif(4))
> c<-matrix(runif(4))
> mylist<-list(a,b,c) # a list of 3 matrices
>
> apply(simplify2array(mylist), c(1,2), mean)
[,1]
[1,] 0.3654349
[2,] 0.4441000
[3,] 0.5745011
[4,] 0.5818541
The vector c(1,2)
for MARGIN
in the apply call indicates that the function mean
should be applied to rows and columns (both at once), see ?apply
for further details.
Another alternative is using Reduce
function
> Reduce("+", mylist)/ length(mylist)
[,1]
[1,] 0.3654349
[2,] 0.4441000
[3,] 0.5745011
[4,] 0.5818541
Mean of each element of matrices in a list
try this:
import numpy as np
a= [[1,2,3],[3,4,5],[6,7,8]]
np.mean(a, axis=0)
# array([3.33333333, 4.33333333, 5.33333333])
How to average the same cells over many lists of matrices in R
Use as.matrix
on the output if you want a matrix class object instead of a data.frame
Reduce('+', x)/length(x)
Finding the mean of multiple arrays in a list
If your data is not too large, you can just concatenate the arrays and then calculate the mean. However, this will create copies of the full dataset, which might need a lot of memory if your data is larger.
In that case, calculate mean and length of each dataset and then do the weighted average of those.
counts = [len(values) for values in cpw]
means = [values.mean() for values in cpw]
mean = np.average(means, weights=counts)
or with concatenation:
np.concatenate(cpw).mean()
element-wise averages of two (or more) nested lists of matrices
In base R, We could use:
A <-list(A_1, A_2)
lapply(Reduce(\(x, y)Map('+', x, y), A), '/', length(A))
$a
[,1] [,2] [,3] [,4]
[1,] 5.5 7.5 9.5 11.5
[2,] 6.5 8.5 10.5 12.5
$b
[,1] [,2] [,3] [,4]
[1,] 3.5 5.5 7.5 9.5
[2,] 4.5 6.5 8.5 10.5
This code is generic in that we can use to find the mean of several lists.
Note that A_1
and A_2
must have the same number of matrices, not necessarily 2. Can be 10 etc. Also note that each corresponding matrix has the same dimensions. Example below:
B_1 <- list(matrix(c(1,2,3,4), 2), matrix(c(1,3,4,2), 2),
matrix(c(1:10), 5), matrix(c(1:20), 5))
B_2 <- lapply(B_1, '*', 2) # In this case, its B_1 * 2
B_3 <- lapply(B_2, '*', 3) #
Now you could use the code provide above:
B <-list(B_1, B_2, B_3)
lapply(Reduce(\(x, y)Map('+', x, y), B), '/', length(B))
Related Topics
How to Use Objects from Global Environment in Rstudio Markdown
Twitter, Roauth and Windows: Register Ok, But Certificate Verify Failed
R Knitr Chunk Options for Figure Height/Width Are Not Working
How to Display All X Labels in R Barplot
For Loop Over Dygraph Does Not Work in R
Return Index from a Vector of the Value Closest to a Given Element
How to Connect R with Access Database in 64-Bit Window
Remove Columns from Dataframe Where Some of Values Are Na
Lib Unspecified & Error in Loadnamespace
Display Custom Image as Geom_Point
Options for Caching/Memoization/Hashing in R
How to Produce Stacked Bars Within Grouped Barchart in R
Concatenate Unique Strings After Groupby in R
Re-Ordering Factor Levels in Data Frame
How to Remove Empty Factors from Ggplot2 Facets
How to Count the Frequency of a String for Each Row in R