Matrix of averages from array

主宰稳场 提交于 2021-02-05 06:43:26

问题


Right now i have an array that is three dimensions. I have 200 rows, 200 columns and 24 "slices" in the third dimension

dim=c(200,200,24)

What I need is an average of the slices resulting in a new matrix. I need a 200 by 200 matrix and the values are the result of averaging up the appropriate slices. So in the location that would be row 1, col 1, I need the average of all the row 1's and col 1's from my array.

Is there a way to do this?


回答1:


Here's one attempt using a simple example:

test <- 1:8
dim(test) <- c(2,2,2)

, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

Get your answer:

apply(test,c(1,2),mean)

     [,1] [,2]
[1,]    3    5
[2,]    4    6


来源:https://stackoverflow.com/questions/15170668/matrix-of-averages-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!