---
title: "ManhattanDistance"
language: "en"
type: "Symbol"
summary: "ManhattanDistance[u, v] gives the Manhattan or city block distance between vectors u and v."
keywords: 
- city block distance
- cityblock
- grid distance
- Manhattan distance
- Manhattan metric
- taxi cab distance
- taxicab distance
- taxicab metric
- taxicab geometry
canonical_url: "https://reference.wolfram.com/language/ref/ManhattanDistance.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Distance and Similarity Measures"
    link: "https://reference.wolfram.com/language/guide/DistanceAndSimilarityMeasures.en.md"
related_functions: 
  - 
    title: "EuclideanDistance"
    link: "https://reference.wolfram.com/language/ref/EuclideanDistance.en.md"
  - 
    title: "SquaredEuclideanDistance"
    link: "https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.en.md"
  - 
    title: "HammingDistance"
    link: "https://reference.wolfram.com/language/ref/HammingDistance.en.md"
  - 
    title: "ChessboardDistance"
    link: "https://reference.wolfram.com/language/ref/ChessboardDistance.en.md"
  - 
    title: "BrayCurtisDistance"
    link: "https://reference.wolfram.com/language/ref/BrayCurtisDistance.en.md"
  - 
    title: "CanberraDistance"
    link: "https://reference.wolfram.com/language/ref/CanberraDistance.en.md"
  - 
    title: "CornerNeighbors"
    link: "https://reference.wolfram.com/language/ref/CornerNeighbors.en.md"
related_tutorials: 
  - 
    title: "Partitioning Data into Clusters"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#2948"
---
# ManhattanDistance

ManhattanDistance[u, v] gives the Manhattan or "city block" distance between vectors u and v.

## Details

* Manhattan distance is effectively the sum of of differences across all dimensions.

* ``ManhattanDistance[u, v]`` is equivalent to ``Total[Abs[u - v]]``. »

## Examples (11)

### Basic Examples (2)

Manhattan distance between two vectors:

```wl
In[1]:= ManhattanDistance[{a, b, c}, {x, y, z}]

Out[1]= Abs[a - x] + Abs[b - y] + Abs[c - z]
```

---

Manhattan distance between numeric vectors:

```wl
In[1]:= ManhattanDistance[{1, 2, 3}, {2, 4, 6}]

Out[1]= 6
```

### Scope (2)

Compute distance between any vectors of equal length:

```wl
In[1]:= ManhattanDistance[RandomReal[5, 100], RandomReal[5, 100]]

Out[1]= 165.47
```

---

Compute distance between vectors of any precision:

```wl
In[1]:= ManhattanDistance[N[{1, 5, 2, 3, 10}, 50], N[{4, 15, 20, 5, 5}, 50]]

Out[1]= 38.00000000000000000000000000000000000000000000000
```

### Applications (2)

Cluster data using Manhattan distance:

```wl
In[1]:= FindClusters[{{2, 3}, {5, 10}, {4, 5}, {2, 2}}, DistanceFunction -> ManhattanDistance]

Out[1]= {{{2, 3}, {2, 2}}, {{4, 5}}, {{5, 10}}}
```

---

Demonstrate the triangle inequality:

```wl
In[1]:= d1 = ManhattanDistance[{a, b}, {a, c}]

Out[1]= Abs[b - c]

In[2]:= d2 = ManhattanDistance[{a, c}, {d, c}]

Out[2]= Abs[a - d]

In[3]:= d3 = ManhattanDistance[{a, b}, {d, c}]

Out[3]= Abs[b - c] + Abs[a - d]

In[4]:= Simplify[d3 <= d1 + d2]

Out[4]= True
```

### Properties & Relations (5)

Manhattan distance is a sum of absolute differences:

```wl
In[1]:= ManhattanDistance[{a, b, c}, {x, y, z}]

Out[1]= Abs[a - x] + Abs[b - y] + Abs[c - z]

In[2]:= Total[Abs[{a, b, c} - {x, y, z}]]

Out[2]= Abs[a - x] + Abs[b - y] + Abs[c - z]
```

---

``ManhattanDistance`` is equivalent to a ``Norm`` of a difference:

```wl
In[1]:= ManhattanDistance[{a, b, c}, {x, y, z}]

Out[1]= Abs[a - x] + Abs[b - y] + Abs[c - z]

In[2]:= Norm[{a, b, c} - {x, y, z}, 1]

Out[2]= Abs[a - x] + Abs[b - y] + Abs[c - z]
```

---

``ManhattanDistance`` is greater than or equal to ``ChessboardDistance`` :

```wl
In[1]:=
u = {a, b, c};
v = {x, y, z};

In[2]:= Simplify[ManhattanDistance[u, v] ≥ ChessboardDistance[u, v]]

Out[2]= True
```

---

``BrayCurtisDistance`` is a ratio of Manhattan distances:

```wl
In[1]:=
u = {a, b, c};
v = {x, y, z};

In[2]:= ManhattanDistance[u, v] / ManhattanDistance[u, -v]

Out[2]= (Abs[a - x] + Abs[b - y] + Abs[c - z]/Abs[a + x] + Abs[b + y] + Abs[c + z])

In[3]:= BrayCurtisDistance[u, v]

Out[3]= (Abs[a - x] + Abs[b - y] + Abs[c - z]/Abs[a + x] + Abs[b + y] + Abs[c + z])
```

---

``MeanDeviation`` as a scaled ``ManhattanDistance`` from the ``Mean`` :

```wl
In[1]:= data = {a, b, c}

Out[1]= {a, b, c}

In[2]:= mean = Table[Mean[data], {Length[data]}]

Out[2]= {(1/3) (a + b + c), (1/3) (a + b + c), (1/3) (a + b + c)}

In[3]:= MeanDeviation[data]

Out[3]= (1/3) (Abs[a + (1/3) (-a - b - c)] + Abs[b + (1/3) (-a - b - c)] + Abs[(1/3) (-a - b - c) + c])

In[4]:= ManhattanDistance[data, mean] / Length[data]

Out[4]= (1/3) (Abs[a + (1/3) (-a - b - c)] + Abs[b + (1/3) (-a - b - c)] + Abs[(1/3) (-a - b - c) + c])
```

## See Also

* [`EuclideanDistance`](https://reference.wolfram.com/language/ref/EuclideanDistance.en.md)
* [`SquaredEuclideanDistance`](https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.en.md)
* [`HammingDistance`](https://reference.wolfram.com/language/ref/HammingDistance.en.md)
* [`ChessboardDistance`](https://reference.wolfram.com/language/ref/ChessboardDistance.en.md)
* [`BrayCurtisDistance`](https://reference.wolfram.com/language/ref/BrayCurtisDistance.en.md)
* [`CanberraDistance`](https://reference.wolfram.com/language/ref/CanberraDistance.en.md)
* [`CornerNeighbors`](https://reference.wolfram.com/language/ref/CornerNeighbors.en.md)

## Tech Notes

* [Partitioning Data into Clusters](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#2948)

## Related Guides

* [Distance and Similarity Measures](https://reference.wolfram.com/language/guide/DistanceAndSimilarityMeasures.en.md)

## History

* [Introduced in 2007 (6.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn60.en.md)