zeke / euclidean-distance

Calculate the Euclidean distance been two points in 2D/3D/nD space.
MIT License
53 stars 10 forks source link

Euclidean Distance Calculation #5

Open nvyin opened 8 years ago

nvyin commented 8 years ago

public static double norm(double[][] dataSet, double[][][] s_pos, int cent, int part, int data_vector) { // Matlab code // distance(data_vector,1)=norm(s_pos(cent,:,part)-Data(data_vector,:))

 System.out.println("========================sps===========================");

    display(s_pos);
    //double[] d1 = {s_pos[cent][0][part], s_pos[cent][1][part]};  // only read one value ?
   // System.out.println("========================d1==========================");
    //System.out.println(Arrays.toString(d1));

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////// convert 3D to 1D ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int rows = s_pos.length; int columns = s_pos[0].length; int depth = s_pos[0][0].length;

double[] d11 = new double[rows * columns * depth];

for (int i = 0; i < depth; i++) {
   for (int j = 0; j < rows; j++) {
      for (int k = 0; k < columns; k++) {
        d11[i + depth*(j + rows*(k))] = s_pos[j][k][i];

         }
     }
  }    

public static double [] convert2dTo1dArray2(double[][] array2d){ double[] result = new double[array2d.length * array2d[0].length];

    for (int i = 0; i < array2d.length; i++) {
        for (int s = 0; s < array2d[0].length; s++) {
            result[(i * array2d[0].length) + s] = array2d[i][s];
        }
    }

    return result;
}   

for (int i = 0; i < d11.length; i++) { squaredSum += Math.pow(d11[i] - d22[i], 2); } return Math.sqrt(squaredSum);

The results are so big compared to matlab function :(

zeke commented 8 years ago

Hi @nvyin. Sorry you're having trouble, but I'm not sure what you are trying to do. Are you trying to write a java program that performs the same operations as this javascript module?

nvyin commented 8 years ago

Dear Zeke,

I am trying to get results which are same as matlab one, the java code that i posted does not produce same result for example distance result in matlab is 0.02 , but in java is 19 ?? i do not know why :(

is the code that iam using have a bug which i not seeing it :(

nvyin commented 8 years ago

@zeke the matlab Norm() manipulating the difference in size and array type in way i do not know i how ? please do you understand my aim ??