如何计算R中的欧几里得距离?
欧几里得空间中两点之间的欧几里得距离是两点之间的线段的长度。它可以使用勾股定理从点的笛卡尔坐标计算,因此有时称为勾股距离。两个向量之间的欧几里得距离由下式给出
√Σ(vect1i - vect2i)2
在哪里,
- vect1 是第一个向量
- vect2 是第二个向量
例如,给定两个向量,vect1 为 (1, 4, 3, 5),vect2 为 (2, 3, 2, 4)。它们的欧几里得距离由 √(1 – 2) 2 + (4 – 3) 2 + (3 – 2) 2 + (5 – 4) 2给出,等于 2。下面是使用两个相等向量的实现长度:
示例 1:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(2, 4, 4, 7)
vect2 <- c(1, 2, 2, 10)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(2, 3, 4, 7)
vect2 <- c(1, 2, 3, 8)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(4, 3, 4, 8)
vect2 <- c(3, 2, 3, 1, 2)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(1, 7, 1, 3, 10, 15 )
vect2 <- c(3, 2, 10, 11 )
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
输出:
示例 2:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(2, 3, 4, 7)
vect2 <- c(1, 2, 3, 8)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
输出:
如果两个向量的长度不相等,则编译器会发出警告消息。下面是使用两个长度不等的向量的实现。
示例 3:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(4, 3, 4, 8)
vect2 <- c(3, 2, 3, 1, 2)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
输出:
正如你在输出中看到的,编译器给了我们一个警告,因为 vect1 的长度比 vect2 短。
示例 4:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(1, 7, 1, 3, 10, 15 )
vect2 <- c(3, 2, 10, 11 )
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
输出:
正如您在输出中看到的,编译器向我们发出警告,因为 vect2 的长度比 vect1 短。