Skip to content

Commit 12c08b0

Browse files
committed
Added Manhattan distance
1 parent 891b752 commit 12c08b0

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Manhattan distance distance implementation
2+
-- See: http://xlinux.nist.gov/dads//HTML/manhattanDistance.html
3+
-- See: http://en.wikipedia.org/wiki/Manhattan_distance
4+
5+
-- Returns the Manhattan distance between two vectors
6+
-- p : a first vector
7+
-- q : a second vector
8+
-- returns : the distance between p and q
9+
local function manhattan_distance(p, q)
10+
assert(#p == #q, 'vectors must have the same length')
11+
local s = 0
12+
for i in ipairs(p) do
13+
s = s + math.abs(p[i] - q[i])
14+
end
15+
return s
16+
end
17+
18+
return manhattan_distance

0 commit comments

Comments
 (0)