gpt4 book ai didi

r - 如何从 dist 给出的单元格返回行号和列号

转载 作者:行者123 更新时间:2023-12-01 09:00:57 25 4
gpt4 key购买 nike

说我有

> x<-1:5
> dist(x)
1 2 3 4
2 1
3 2 1
4 3 2 1
5 4 3 2 1
> which(dist(x)==max(dist(x)))
[1] 4

如何从索引4返回行号和列号(5,1)

最佳答案

可能有更整洁的方式...

dist.x <- dist(x)
which(as.matrix(dist.x) == max(dist.x) & lower.tri(dist.x), arr.ind=TRUE)
# row col
# 5 5 1

关于r - 如何从 dist 给出的单元格返回行号和列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com