manyspikes

Manhattan distance

Initialising environment...

In the previous section, we briefly looked at the Euclidean distance and cosine similarity. In this exercise, you are going to implement a function that computes the Manhattan distance.

The Manhattan distance, also known as the taxicab distance, between two vectors p=(p1,p2,...,pn)\mathbf{p}=(p_1,p_2,...,p_n) and q=(q1,q2,...,qn)\mathbf{q}=(q_1,q_2,...,q_n) is defined as:

dT=i=1npiqi\begin{equation} d_T = \sum_{i=1}^n |p_i - q_i| \end{equation}

where |\cdot| represents the absolute value.

Instructions

Define a function called manhattan_distance, which takes as input two lists of numeric values (representing two vectors) and returns the Manhattan distance between them.