📌  相关文章
📜  如何计算R中的曼哈顿距离?

📅  最后修改于: 2022-05-13 01:54:22.402000             🧑  作者: Mango

如何计算R中的曼哈顿距离?

曼哈顿距离是 N 维向量空间中两点之间的距离度量。它被定义为相应维度中坐标之间的绝对距离之和。

例如,在具有两个点 Point1 (x 1 ,y 1 ) 和 Point2 (x 2 ,y 2 ) 的二维空间中,曼哈顿距离由 |x 1 – x 2 | 给出。 + |y 1 – y 2 |。

方法一:使用公式法

在 R 中,曼哈顿距离是根据向量计算的。两个向量之间的曼哈顿距离由下式给出,

Σ|vect1i - vect2i|

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量

例如,给定两个向量,vect1 为 (3, 6, 8, 9),vect2 为 (1, 7, 8, 10)。他们的曼哈顿距离由 |3 – 1| 给出+ |6 – 7| + |8 – 8| + |9 – 10|等于 4。

下面是使用两个等长向量的实现:

示例 1:

R
# Function to calculate Manhattan distance
# abs() function calculate the absolute difference
# between corresponding vector elements
# sum() function calculates the sum of the
# absolute difference between
# corresponding elements of vect1 and vect2
manhattanDistance <- function(vect1, vect2){
     dist <- abs(vect1 - vect2)
     dist <- sum(dist)
     return(dist)
}
 
# Initializing a vector
vect1 <- c(3, 6, 8, 9)
 
# Initializing another vector
vect2 <- c(1, 7, 8, 10)
 
 
print("Manhattan distance between vect1 and vect2 is: ")
 
# Call the function to calculate Manhattan
# distance between vectors
manhattanDistance(vect1, vect2)


R
# Function to calculate Manhattan distance
# abs() function calculate the absolute difference
# between corresponding vector elements
# sum() function calculates the sum of the
# absolute difference between
# corresponding elements of vect1 and vect2
manhattanDistance <- function(vect1, vect2){
     dist <- abs(vect1 - vect2)
     dist <- sum(dist)
     return(dist)
}
 
# Initializing two vectors having unequal length
vect1 <- c(14, 13, 24, 18)
vect2 <- c(13, 12, 33, 11, 12)
 
print("Manhattan distance between vect1 and vect2 is: ")
 
# Call the function to calculate Manhattan distance
manhattanDistance(vect1, vect2)


R
# Initializing a vector
vect1 < - c(1, 16, 8, 10, 100, 20)
 
# Initializing another vector
vect2 < - c(1, 7, 18, 90, 50, 21)
 
# Initializing another vector
vect3 < - c(3, 10, 11, 40, 150, 210)
 
 
# Initializing another vector
vect4 < - c(2, 1, 4, 7, 8, 10)
 
 
# Initializing another vector
vect5 < - c(1, 4, 8, 3, 100, 104)
 
# Initializing another vector
vect6 < - c(3, 7, 11, 23, 110, 114)
 
 
# Row bind vectors into a single matrix
twoDimensionalVect < - rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate manhattan distance between
# each unique pair of vectors
# That is why we are passing manhattan as a method
dist(twoDimensionalVect, method="manhattan")


R
# Initializing a vector
vect1 <- c(4, 3, 5, 7, 8, 2, 10, 12)
 
# Initializing another vector
vect2 <- c(5, 9, 4, 9, 7, 17)
 
# Initializing another vector
vect3 <- c(3, 10, 9, 11, 13, 12)
 
 
# Initializing another vector
vect4 <- c(4, 7, 6, 12, 10, 12)
 
 
# Initializing another vector
vect5 <- c(3, 5, 12, 10, 1, 17)
 
# Initializing another vector
vect6 <- c(4, 3, 1, 8, 7, 2)
 
 
# Using rind function to bind vectors in a 2-d vector
# Note that all vectors are not of the same length
twoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Manhattan distance
# between each pair of vectors
# That is why we are passing "manhattan" as a method
dist(twoDimensionalVect, method = "manhattan")


输出:

[1] "Manhattan distance between vect1 and vect2 is: "
[1] 4

示例 2:

如果两个向量的长度不相等,则编译器会发出警告消息。下面是使用两个长度不等的向量的实现。

R

# Function to calculate Manhattan distance
# abs() function calculate the absolute difference
# between corresponding vector elements
# sum() function calculates the sum of the
# absolute difference between
# corresponding elements of vect1 and vect2
manhattanDistance <- function(vect1, vect2){
     dist <- abs(vect1 - vect2)
     dist <- sum(dist)
     return(dist)
}
 
# Initializing two vectors having unequal length
vect1 <- c(14, 13, 24, 18)
vect2 <- c(13, 12, 33, 11, 12)
 
print("Manhattan distance between vect1 and vect2 is: ")
 
# Call the function to calculate Manhattan distance
manhattanDistance(vect1, vect2)

输出:

方法二:使用 dist()函数

R 提供了一个内置函数,使用它我们可以找到二维向量中每个唯一向量对之间的曼哈顿距离。

句法:

示例 1:

下面是使用 dist()函数查找曼哈顿距离的实现:

R

# Initializing a vector
vect1 < - c(1, 16, 8, 10, 100, 20)
 
# Initializing another vector
vect2 < - c(1, 7, 18, 90, 50, 21)
 
# Initializing another vector
vect3 < - c(3, 10, 11, 40, 150, 210)
 
 
# Initializing another vector
vect4 < - c(2, 1, 4, 7, 8, 10)
 
 
# Initializing another vector
vect5 < - c(1, 4, 8, 3, 100, 104)
 
# Initializing another vector
vect6 < - c(3, 7, 11, 23, 110, 114)
 
 
# Row bind vectors into a single matrix
twoDimensionalVect < - rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate manhattan distance between
# each unique pair of vectors
# That is why we are passing manhattan as a method
dist(twoDimensionalVect, method="manhattan")

输出:

示例 2:

请注意,二维向量下呈现的所有向量的长度必须相同,否则,R 编译器会产生编译器时错误。

R

# Initializing a vector
vect1 <- c(4, 3, 5, 7, 8, 2, 10, 12)
 
# Initializing another vector
vect2 <- c(5, 9, 4, 9, 7, 17)
 
# Initializing another vector
vect3 <- c(3, 10, 9, 11, 13, 12)
 
 
# Initializing another vector
vect4 <- c(4, 7, 6, 12, 10, 12)
 
 
# Initializing another vector
vect5 <- c(3, 5, 12, 10, 1, 17)
 
# Initializing another vector
vect6 <- c(4, 3, 1, 8, 7, 2)
 
 
# Using rind function to bind vectors in a 2-d vector
# Note that all vectors are not of the same length
twoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Manhattan distance
# between each pair of vectors
# That is why we are passing "manhattan" as a method
dist(twoDimensionalVect, method = "manhattan")

输出: