mathajax

Metric - Manhattan Distance between two points

The java program finds distance between two points using manhattan distance equation.  The points can be a scalar or vector and the passed to function as arguments can be integer or double datatype.


1D distance between two points
It measures distance between two points on a line (1D) by absolute difference between them and the points are scalar value.






Two points on 2D cartesian space
It measures distance between two points on a plane. The points are vectors and each has two elements. The resulting distance equals sum of the difference of each element on the vectors.







Two points on N-Dimensional cartesian space
It measures distance between two points or vectors on N-dimensional space. The points are vectors and each has N elements.The resulting distance equals sum of the difference of each element on the vectors.











This is an example calculation shown below explain how to find the distance between two vectors using Manhattan distance formula. A vector,array of elements declared and initialized in Java using one dimensional array.


        int A[] = { 2 ,3 };    // 2D vector A 
 
 int B[] = { 4,1 };    // 2D vector B
 
   

find distance between two vector A and B using Manhattan distance equation.

||A-B|| = abs(A[0]-B[0]) + abs(A[1]-B[1]) = abs(2-4) + abs(3-1) ); = abs(-2) + abs(2) ; = 2 + 2 ; ||A-B|| = 4 ; Manhattan Distance between vectors A=[2,3] and B=[4,1] is 4



public class Manhattan {

 public static double distance(double x, double y) {  
  return  Math.abs( x-y );  
 }
  
 public static double distance(int x, int y) {  
  return Math.abs(x-y);          
 }
 
 
 public static double distance(double x[], double y[]) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.abs(x[n]-y[n]);   
  return  ds;  
 }
  
 public static double distance(int x[], int y[]) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.abs(x[n]-y[n]);   
  return  ds;            
 }
 
 
  public static double[] distance(int x[][], int y[]) {
  
    double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n]= distance(x[n],y);   
  return  ds;            
 }
 
  public static double[] distance(double x[][], double y[]) {
  
  double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n] = distance(x[n],y);   
  return  ds;            
 }

 
}




package metric;
import java.util.Arrays;
public class ManhattanMain {

 public static void main(String[] args) 
  {
   
 System.out.println("1D - Distance on integer" );
 System.out.println("\nManhattan Distance between scalar 
                          int x and y " );
 int ix=20; 
 int iy=30;          
 double dist=Manhattan.distance(ix, iy);
 System.out.println("x="+ix +",y="+iy);
 System.out.println("Distance :" + dist);
  
 System.out.println("1D - Distance on double" );
        System.out.println("\nManhattan Distance between scalar 
                           double x and  y" );
        double dx=2.6; 
        double dy=3.2;          
        double ds2=Manhattan.distance(dx, dy);
        System.out.println("x="+dx +",y="+dy);
        System.out.println("Distance :" + ds2);
     
  
       System.out.println("2D - Distance on double" );
       System.out.println("\nManhattan Distance between 
       vector int x and y" );
       int aix[]={2,3};
       int aiy[]={3,5};          
       double ds3=Manhattan.distance(ix, iy);     
       System.out.println("x="+Arrays.toString(aix)
                           +",y="+Arrays.toString(aiy));
       System.out.println("Distance :" + ds3);
     
        System.out.println("2D - Distance on double" );
        System.out.println("\nManhattan Distance between vector 
                  double x and y " );
        double adx[]={2.4,3.1,5.2};
        double ady[]={3.1,5.6,7.8};      
        System.out.println("x="+Arrays.toString(adx) 
               +",y="+Arrays.toString(ady));
        double ds4=Manhattan.distance(adx, ady);
        System.out.println("Distance :" + ds4);
     
        System.out.println("\nManhattan Distance between set of 
        vector int x and a vector int y " );
        int aix2[][]={ {2,3,5} ,{1,7,4}};
        int aiy2[]={3,5,7};          
        double ds5[]=Manhattan.distance(aix2, aiy2);     
        System.out.println("x="+Arrays.toString(aix2[0]) + "," 
            + Arrays.toString(aix2[1]) +",y="+Arrays.toString(aiy2));
        System.out.println( "Distance :" + Arrays.toString(ds5) );
     
     
     
        System.out.println("\nManhattan Distance between set of 
               double vector x and a vector y " );
        double adx2[][]={ {2.4,3.1,5.2},{1.2,7.2,4.5}};
        double ady2[]={3.1,5.6,7.8};          
        double ds6[]=Manhattan.distance(adx2, ady2);     
        System.out.println("x="+ Arrays.toString(adx2[0])+"," 
            +Arrays.toString(adx2[1])  +",y="+Arrays.toString(ady2));
       System.out.println( "Distance :" + Arrays.toString(ds6) );   
 
  }

}


Output
1D - Distance on integer
Manhattan Distance between scalar int x and y 
x=20,y=30
Distance :10.0

1D - Distance on double
Manhattan Distance between scalar double x and  y
x=2.6,y=3.2
Distance :0.6000000000000001

2D - Distance on double
Manhattan Distance between vector int x and y
x=[2, 3],y=[3, 5]
Distance :3.0

2D - Distance on double
Manhattan Distance between vector double x and y 
x=[2.4, 3.1, 5.2],y=[3.1, 5.6, 7.8]
Distance :5.799999999999999

Manhattan Distance between set of vector int x and a vector int y 
x=[2, 3, 5],[1, 7, 4],y=[3, 5, 7]
Distance :[5.0, 7.0]

Manhattan Distance between set of double vector x and a vector y 
x=[2.4, 3.1, 5.2],[1.2, 7.2, 4.5],y=[3.1, 5.6, 7.8]
Distance :[5.799999999999999, 6.800000000000001]




Comments

Post a Comment

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement