Reputation: 1
I have m <- a 100*6 matrix
. I want to return an 6*6 matrix
, and the entry(i,j)of the 6*6 matrix
contain the following value:
(mean(col.i)-mean(col.j))/sd(col.i and col.j)
where sd(col.i and col.j) is the standard deviation of all values from both col.i and col.j
I want to use apply function to do this, but I don't know how to combine each two columns of matrix m. So how can I get the 6*6 matrix? what function should I use?(in r)
Upvotes: 0
Views: 92
Reputation: 24500
You can create all the combos of the i,j
indices through expand.grid
and then use mapply
to obtain any element of your matrix. Something like this:
#generate a sample matrix
m<-matrix(runif(600),ncol=6)
#generate the indices
indices<-expand.grid(1:6,1:6)
#the result
res<-matrix(mapply(function (x,y) (mean(m[,x])-mean(m[,y]))/sd(m[,c(x,y)]),indices[[1]],indices[[2]]),ncol=6)
Upvotes: 2
Reputation: 26
Without any details on the platform/language you are using, I can give the following general suggestions: 1. Find the means of the elements in each column. 2. Find the sum of squares of the elements in each column. You can then use the formula given at https://en.wikipedia.org/wiki/Standard_deviation#Identities_and_mathematical_properties to get the standard deviations that you need. The formula is:
standard deviation = sqrt(1/N * (sum of squares) - (square of the mean))
Upvotes: -1