2

I have 13 Area of Interests (AOIs) with x and y values for each test image. How can I get all possible combination of euclidian distance between pairs of AOIs, ((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.

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
theduck
  • 2,589
  • 13
  • 17
  • 23
Hyokstayo
  • 33
  • 3

1 Answers1

2

See dist(). Since not enough test data was provided, here's an example on 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]
Cole
  • 11,130
  • 1
  • 9
  • 24