matrix - Elementwise median of list of matrices in R -


given list of matrices:

temp <- list(matrix(c(1,8,3,400), 2),      matrix(c(5,2,300,14),2),      matrix(c(100,200,12,4),2) ) temp # [[1]] #      [,1] [,2] # [1,]    1    3 # [2,]    8  400 # # [[2]] #      [,1] [,2] # [1,]    5  300 # [2,]    2   14 # # [[3]] #      [,1] [,2] # [1,]  100   12 # [2,]  200    4 

i want element-wise median of matrices:

     [,1] [,2] [1,]    5   12 [2,]    8   14 

can done without explicit loops?

first, put array:

library(abind) <- do.call(abind, c(temp, list(along=3))) 

then use apply:

apply(a, 1:2, median) #      [,1] [,2] # [1,]    5   12 # [2,]    8   14 

as @richardscriven suggests, can build a without abind package:

a <- array(unlist(temp), c(2, 2, 3)) # or <- array(unlist(temp), c(dim(temp[[1]]), length(temp))) 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -