R - 将矩阵中的每个值除以其行/列的最大值

Mon*_*Mon 8 database r rows matrix

我试图通过将每个值除以其列或行名称的最大值中的较小值来转换矩阵中的值.我遇到了麻烦,因为我不知道如何从较大的函数内查询特定值的行/列.

一小部分数据如下所示:加权(对称)邻接矩阵,mat:

              Acousmatic Acoustic Afro-beat Alternative Ambient
  Acousmatic         125       11         3           3       1
  Acoustic            11   112398      1810       24216    3824
  Afro-beat            3     1810     10386        1220     298
  Alternative          3    24216      1220      103286    2838
  Ambient              1     3824       298        2838   20400
Run Code Online (Sandbox Code Playgroud)

作为一个例子,我想通过找到由其对角线给出的"声学"的最大值(112398)和由其对角线给出的"替代"的最大值来转换"替代声学"(24216)的值(103286) ,并将"Alternative-Acoustic"(24216)除以这两个数字中的较小者.所以在这种情况下,较小的将是"替代",所以我想用24216/103286 =〜.2345转换"Alternative-Acoustic"值.

我想自动对此矩阵中的所有值执行此变换,这将导致矩阵的值范围为0-1,对角线为全1.

我在许多不同的迭代中尝试了以下内容,其中"mat"作为矩阵和数据框,但我不知道如何正确查询矩阵中每个值的行和列最大值.这是使用不存在的函数('colmax'和'rowmax'),但我认为它最清楚地表达了我想要做的事情:

transformedmat < - apply(mat,1:2,function(x)x/min(colmax(x),rowmax(x)))

我也尝试编写一个嵌入式函数,但结果很差,我想知道是否有一个更简单的解决方案:

rescalemat <- function(mat){
    apply(mat, 1, function(x){
    colmax<-apply(mat, 2, function(x) max(x))
    rowmax<-apply(mat, 1, function(x) max(x))
    x/min(colmax,rowmax)
    mat
})
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

谢谢.

ale*_*laz 6

除非我错过了什么,否则这种方法看起来也是有效的:

res = diag(mat)
#names(res) = colnames(mat)       
mat / outer(res, res, pmin) 

#            Acousmatic  Acoustic  Afro.beat Alternative    Ambient
#Acousmatic       1.000 0.0880000 0.02400000   0.0240000 0.00800000
#Acoustic         0.088 1.0000000 0.17427306   0.2344558 0.18745098
#Afro-beat        0.024 0.1742731 1.00000000   0.1174658 0.02869247
#Alternative      0.024 0.2344558 0.11746582   1.0000000 0.13911765
#Ambient          0.008 0.1874510 0.02869247   0.1391176 1.00000000
Run Code Online (Sandbox Code Playgroud)

在哪里mat:

mat = structure(c(125L, 11L, 3L, 3L, 1L, 11L, 112398L, 1810L, 24216L, 
3824L, 3L, 1810L, 10386L, 1220L, 298L, 3L, 24216L, 1220L, 103286L, 
2838L, 1L, 3824L, 298L, 2838L, 20400L), .Dim = c(5L, 5L), .Dimnames = list(
    c("Acousmatic", "Acoustic", "Afro-beat", "Alternative", "Ambient"
    ), c("Acousmatic", "Acoustic", "Afro.beat", "Alternative", 
    "Ambient")))
Run Code Online (Sandbox Code Playgroud)


ami*_*mit 5

尝试这个:

A1 = mat/apply(mat,1,max)
A2 = t(t(mat)/apply(mat,2,max))
result = ifelse(A1>A2,A1,A2)
Run Code Online (Sandbox Code Playgroud)

  • 最后一步可以是“ pmax(A1,A2)”。 (2认同)