Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

r - R中13个行索引之间的欧式距离置换(Permutation of euclidean distances between 13 row indexes in R)

I have 13 Area of Interests (AOIs) with x and y values for each test image.

(对于每个测试图像,我都有13个兴趣区域(AOI),分别具有x和y值。)

How can I get all possible combination of euclidian distance between pairs of AOIs, ((x-xi)+(y-yi))^(1/2)?

(如何获得AOI对之间((x-xi)+(y-yi))^(1/2)的欧氏距离的所有可能组合?)

Ultimately I am looking for the maximum distance from all possible distances between two AOIs for each test image.

(最终,我正在为每个测试图像从两个AOI之间的所有可能距离中寻找最大距离。)

Can this be done without using loops?

(不用循环就可以做到吗?)

> 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
  ask by Hyokstayo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See dist() .

(参见dist() 。)

Since not enough test data was provided, here's an example on iris :

(由于没有提供足够的测试数据,因此以下是关于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 

And a similar approach in

(和的类似方法)

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

And :

(和 :)

library(data.table)
as.data.table(iris)[,
                    .(max_dist = max(dist(.SD))),
                    .SDcols = c('Sepal.Length', 'Sepal.Width'),
                    by = Species]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...