Answering "What is Manhattan distance?"
Re: http://jsoftware.com/pipermail/programming/2018-December/052413.html

┌─┬─┬─┬─┬─┬─┬─┐
│ │ │ │ │ │ │ │
├─┼─┼─B─┼─┼─┼─┤
│ │ │ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │
├─┼─┼─┼─A─┼─┼─┤
│ │ │ │ │ │ │ │
└─┴─┴─┴─┴─┴─┴─┘
Manhattan distance is the number of blocks you have to walk to get from here to there in the big apple.
Distance is scalar: the distance from A to B is a single number.
The tally of blocks is the same whether you walk from A to B or from B to A.
one block across the avenues, two blocks across the numbered streets: three 
blocks.

That the distance is scalar value provides a plausibility test for the answer.  
If my algorithm produces something other than a single number for the distance 
I know it is incorrect.

That the Manhattan distance is independent of direction explains why absolute 
value appears in my definition.


NB. use: given location vectors for two points A and B
NB.      the manhattan distance between them is
NB.      A manhattan B
NB. example:
NB.    assert 3 = 3 3 manhattan 4 1

manhattan =: dyad define
  vector_from_x_to_y =. y - x
  independent_lengths =. | vector_from_x_to_y
  +/ independent_lengths
)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to