简体   繁体   English

R中矩阵运算的功能

[英]Functions to operate matrix in r

I have m <- a 100*6 matrix . 我有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: 我想返回一个6*6 matrix ,以及入境(I,J) 6*6 matrix包含以下值:

(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 其中sd(col.i和col.j)是来自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. 我想使用apply函数来执行此操作,但是我不知道如何组合矩阵m的每两列。 So how can I get the 6*6 matrix? 那么如何获得6 * 6矩阵呢? what function should I use?(in r) 我应该使用什么功能?(在r中)

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. 您可以通过expand.grid创建i,j索引的所有组合,然后使用mapply获取矩阵的任何元素。 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)

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. 在没有所使用平台/语言的任何详细信息的情况下,我可以给出以下一般建议:1.在每一列中找到元素的含义。 2. Find the sum of squares of the elements in each column. 2.在每一列中找到元素的平方和。 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. 然后,您可以使用https://en.wikipedia.org/wiki/Standard_deviation#Identities_and_mathematical_properties中给出的公式来获取所需的标准偏差。 The formula is: 公式为:

standard deviation = sqrt(1/N * (sum of squares) - (square of the mean)) 标准偏差= sqrt(1 / N *(平方和)-(平均值的平方))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM