[英]How to find the min/max for matrix columns with specifided indexes?
I reading the paper and try to implement the algorithm 2:我阅读了论文并尝试实现算法2:
where ave(x,y)=2xy/(x+y).其中ave(x,y)=2xy/(x+y)。
I have found D1
, D2
:我找到了
D1
, D2
:
> dput(D1)
structure(c(0, 0, 0.316227766016838, 0.316227766016838, 0.316227766016838,
0, 0, 0.316227766016838, 0.316227766016838, 0.316227766016838,
0.316227766016838, 0.316227766016838, 0, 0.316227766016838, 0.447213595499958,
0.316227766016838, 0.316227766016838, 0.316227766016838, 0, 0.316227766016838,
0.316227766016838, 0.316227766016838, 0.447213595499958, 0.316227766016838,
0), .Dim = c(5L, 5L), .Dimnames = list(c("1", "4", "5", "7",
"9"), c("1", "4", "5", "7", "9")))
> dput(D2)
structure(c(0, 0.447213595499958, 0.316227766016838, 0, 0.447213595499958,
0.447213595499958, 0, 0.316227766016838, 0.447213595499958, 0,
0.316227766016838, 0.316227766016838, 0, 0.316227766016838, 0.316227766016838,
0, 0.447213595499958, 0.316227766016838, 0, 0.447213595499958,
0.447213595499958, 0, 0.316227766016838, 0.447213595499958, 0
), .Dim = c(5L, 5L), .Dimnames = list(c("1", "5", "7", "8", "10"
), c("1", "5", "7", "8", "10")))
Then I defined the i, j and r indexes:然后我定义了 i、j 和 r 索引:
> dput(i)
c("4", "9")
> dput(j)
c("8", "10")
> dput(r)
c("1", "5", "7")
and finally最后
k = length(I)
D = matrix(0, k, k)
x = min( D1[i, r] + D2[j, r])
y = max(abs(D1[i, r] - D2[j, r]))
D = 2*x*y/(x+y)
The exected answer is symmetric 2x2 matrix, but I have执行的答案是对称的 2x2 矩阵,但我有
> D
[1] 0
Also, I have tried to use the apply
function:另外,我尝试使用
apply
function:
x = apply( D1+D2, 1, function(x) min(x))
y = apply(abs( D1-D2 ), 1, function(x) max(x))
D = 2*x*y/(x+y)
The answer is the vector:答案是向量:
> D
1 4 5 7 9
0 0 0 0 0
Question.问题。 How to find the min(), max() for columns with specified indexes?
如何找到具有指定索引的列的 min()、max()?
Try the for
loops like below试试下面的
for
循环
D <- `dimnames<-`(matrix(NA, length(i),length(j)),list(i,j))
for (ki in i) {
for (kj in j) {
x <- min(D1[ki,r] + D2[kj,r])
y <- max(abs(D1[ki,r] - D2[kj,r]))
D[ki,kj] <- 2*x*y/(x+y)
}
}
and you will get你会得到
> D
8 10
4 0.0000000 0.3704839
9 0.3162278 0.4472136
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.