R中13个行索引之间的欧式距离置换

问题描述 投票:-1回答:1

我有13个兴趣区域(AOI),每个测试图像的x和y值。如何获得AOI对之间((x-xi)+(y-yi))^(1/2)的欧氏距离的所有可能组合?最终,我正在为每个测试图像从两个AOI之间的所有可能距离中寻找最大距离。

可以在不使用循环的情况下完成此操作吗?

> setwd("C:/Users/Data/Desktop")
> RawColor <- read.csv(file="13ptColor.csv")
> print(RawColor)
            SN TestImage AOI      x       y
1     50293253         B  13 0.1597 0.06775
2     50293253         B  12 0.1587 0.06574
3     50293253         B  11 0.1596 0.06715
4     50293253         B  10 0.1594 0.06618
5     50293253         B   9 0.1590 0.06582
6     50293253         B   8 0.1593 0.06638
7     50293253         B   7 0.1589 0.06602
8     50293253         B   6 0.1594 0.06601
9     50293253         B   5 0.1591 0.06552
10    50293253         B   4 0.1587 0.06473
11    50293253         B   3 0.1593 0.06603
12    50293253         B   2 0.1585 0.06481
13    50293253         B   1 0.1588 0.06510
14    50293253         G  13 0.2985 0.60400
15    50293253         G  12 0.2977 0.60440
r dplyr max permutation euclidean-distance
1个回答
0
投票

请参见dist()。由于没有提供足够的测试数据,因此下面是iris上的示例:

as.matrix(
  by(data = iris[, c('Sepal.Length', 'Sepal.Width')], 
     INDICES = iris[, 'Species', drop = F], 
     FUN = function(DF) max(dist(DF))
     )
  )

#               [,1]
# setosa     2.418677
# versicolor 2.332381
# virginica  3.269557

# or
sp_DF <- split(x = iris[, c('Sepal.Length', 'Sepal.Width')],
               f = iris[, 'Species', drop = F])

sapply(sp_DF, function(DF) max(dist(DF)))

#    setosa versicolor  virginica 
#  2.418677   2.332381   3.269557 

以及中的类似方法>

library(dplyr)

iris%>%
  group_by(Species)%>%
  summarize(max_dist = max(dist(cbind(Sepal.Length, Sepal.Width))))

# A tibble: 3 x 2
  Species    max_dist
  <fct>         <dbl>
1 setosa         2.42
2 versicolor     2.33
3 virginica      3.27

library(data.table)
as.data.table(iris)[,
                    .(max_dist = max(dist(.SD))),
                    .SDcols = c('Sepal.Length', 'Sepal.Width'),
                    by = Species]
© www.soinside.com 2019 - 2024. All rights reserved.