0
votes

This question is quite basic: I'm very confused by the documentation for rowVars in the package MatrixStats in R.

I have an array of dimensions (12, 12, 10000), ie 10000 12x12 matrices. rowMeans very easily gives me the mean of each row of each matrix in the form of a list with 10000 items. I want to do the same with rowVars to get variances.

This works fine for a single matrix, but for anything with more dimensions it gives an error message saying to use the dim argument, and I don't understand how it works. The package documentation says that dim is "An integer vector of length two specifying the dimension of x, also when not a matrix." (where x is the object the function is to be used on). However, I don't understand what this means, and haven't been able to find any helpful examples of it in use. What does 'specifying the dimension' mean- specifying how many dimensions? or specifying the size of each eg (12, 12, 10000)? If so, how can it be of length 2?

Thank you!

1

1 Answers

0
votes

The documentation for rowVars states that the input x should be a "numeric N x K matrix," so it sounds like this function only supports two-dimensional matrices. If you want the variances of each row for your 10000 matrices, you could instead do something like this:

mat <- array(rnorm(12*12*10000,0,1),dim=c(12,12,10000))
rvs <- sapply(1:10000,function(x) rowVars(mat[,,x]))

The resulting object rvs will be a matrix where the nth column is the row variances for the nth 12 x 12 matrix.