从矩阵中挑选阈值

时间:2017-01-04 01:47:29

标签: r matrix

我有一个名为D的矩阵,我想从D中选择一个特定值作为阈值。

d

     [,1]     [,2]     [,3]     [,4]      [,5]       [,6]       [,7]
 [1,] 4.20523 0.000000 0.000000 0.000000 0.0000000 0.00000000 0.00000000
 [2,] 0.00000 2.161999 0.000000 0.000000 0.0000000 0.00000000 0.00000000
 [3,] 0.00000 0.000000 1.138677 0.000000 0.0000000 0.00000000 0.00000000
 [4,] 0.00000 0.000000 0.000000 1.040475 0.0000000 0.00000000 0.00000000
 [5,] 0.00000 0.000000 0.000000 0.000000 0.3852305 0.00000000 0.00000000
 [6,] 0.00000 0.000000 0.000000 0.000000 0.0000000 0.04953807 0.00000000
 [7,] 0.00000 0.000000 0.000000 0.000000 0.0000000 0.00000000 0.01362526
 [8,] 0.00000 0.000000 0.000000 0.000000 0.0000000 0.00000000 0.00000000
 [9,] 0.00000 0.000000 0.000000 0.000000 0.0000000 0.00000000 0.00000000
             [,8]         [,9]
 [1,] 0.000000000 0.000000e+00
 [2,] 0.000000000 0.000000e+00
 [3,] 0.000000000 0.000000e+00
 [4,] 0.000000000 0.000000e+00
 [5,] 0.000000000 0.000000e+00
 [6,] 0.000000000 0.000000e+00
 [7,] 0.000000000 0.000000e+00
 [8,] 0.005127803 0.000000e+00
 [9,] 0.000000000 9.693644e-05

我用过

其中(DIAG(d)< 0.56)

[1] 5 6 7 8 9

如上所示。我想将第5个对角线元素固定为我的epsilon。也就是说,我的epsilon = 0.3852305。

我不想每次都查看矩阵并像epsilon = D [5,5]那样修复epsilon。

我也尝试过使用ifelse语句

ifelse(D> 0& D< 0.5,1,0)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    0    0    0    0    0    0    0    0
 [2,]    0    0    0    0    0    0    0    0    0
 [3,]    0    0    0    0    0    0    0    0    0
 [4,]    0    0    0    0    0    0    0    0    0
 [5,]    0    0    0    0    1    0    0    0    0
 [6,]    0    0    0    0    0    1    0    0    0
 [7,]    0    0    0    0    0    0    1    0    0
 [8,]    0    0    0    0    0    0    0    1    0
 [9,]    0    0    0    0    0    0    0    0    1

从此我如何将epsilon值修复为我的阈值?

提前致谢。

2 个答案:

答案 0 :(得分:0)

这应该返回你的门槛:

thres <- D[which(D>0 & D<0.5, arr.ind = TRUE)][1]
thres
# [1] 0.3852305

答案 1 :(得分:0)

如果您只需要该值,那么这就够了

diag(D)[which(diag(D) < 0.56)][1]