private static void computeCorrelationMatrix(int rank,double[][] vectors,Distances distances){
  double[] mean=new double[rank];
  for (  double[] row : vectors) {
    for (int col=0; col < rank; col++) {
      mean[col]+=row[col];
    }
  }
  for (int col=0; col < rank; col++) {
    mean[col]/=vectors.length;
  }
  double[] stddev=new double[rank];
  for (  double[] row : vectors) {
    for (int col=0; col < rank; col++) {
      stddev[col]+=(row[col] - mean[col]) * (row[col] - mean[col]);
    }
  }
  for (int col=0; col < rank; col++) {
    stddev[col]=Math.sqrt(stddev[col] / vectors.length);
  }
  for (int di=0; di < rank; di++) {
    distances.set(di + 1,di + 1,0);
    for (int dj=di + 1; dj < rank; dj++) {
      double cor=0;
      for (      double[] row : vectors) {
        cor+=(row[di] - mean[di]) * (row[dj] - mean[dj]) / (stddev[di] * stddev[dj]);
      }
      cor/=vectors.length;
      distances.set(di + 1,dj + 1,cor);
    }
  }
}

private static void computeCorrelationMatrix ( int rank , double [ ] [ ] vectors , Distances distances ) { double [ ] mean = new double [ rank ] ; for ( double [ ] row : vectors ) { for ( int col = 0 ; col < rank ; col ++ ) { mean [ col ] += row [ col ] ; } } for ( int col = 0 ; col < rank ; col ++ ) { mean [ col ] /= vectors . length ; } double [ ] stddev = new double [ rank ] ; for ( double [ ] row : vectors ) { for ( int col = 0 ; col < rank ; col ++ ) { stddev [ col ] += ( row [ col ] - mean [ col ] ) * ( row [ col ] - mean [ col ] ) ; } } for ( int col = 0 ; col < rank ; col ++ ) { stddev [ col ] = Math . sqrt ( stddev [ col ] / vectors . length ) ; } for ( int di = 0 ; di < rank ; di ++ ) { distances . set ( di + 1 , di + 1 , 0 ) ; for ( int dj = di + 1 ; dj < rank ; dj ++ ) { double cor = 0 ; for ( double [ ] row : vectors ) { cor += ( row [ di ] - mean [ di ] ) * ( row [ dj ] - mean [ dj ] ) / ( stddev [ di ] * stddev [ dj ] ) ; } cor /= vectors . length ; distances . set ( di + 1 , dj + 1 , cor ) ; } } }
computes the pearson distances
