Example: Dissimilarities

The following example illustrates the use of Dissimilarities for computing the Euclidean distance between the rows of a matrix:

import java.io.*;
import com.imsl.stat.*;
import com.imsl.math.*;

public class DissimilaritiesEx1 {
    public static void main(String argv[]) throws Exception {
        double[][] x = {
            { 1., 1.},
            { 1., 0.},
            { 1., -1.},
            { 1., 2.}};
        int distanceMethod = 0;
        int distanceScale = 0;
        int iRow = 1;

            Dissimilarities dist = 
               new Dissimilarities(x, distanceMethod, distanceScale, iRow);
            double[][] distanceMatrix = dist.getDistanceMatrix();

            for (int i=0;i<distanceMatrix.length;i++){
                for (int j=0;j<distanceMatrix[0].length;j++)
                    System.out.print(distanceMatrix[i][j]+", ");
                System.out.println();
            }
    }
}

Output

0.0, 1.0, 2.0, 1.0, 
0.0, 0.0, 1.0, 2.0, 
0.0, 0.0, 0.0, 3.0, 
0.0, 0.0, 0.0, 0.0, 
Link to Java source.