Java 二维数组比较
Java 2d Array Comparision
你好,我想问一下关于二维数组比较的问题。
我在每行中都有以下带有双参数的二维数组。
我想比较前 5 行(即:Period 0)和 secont 5 行(即:Period1)
我想比较
比较将逐行作为一维数组我需要两个一维数组来相互比较..
例子;
周期 0(第 0 行)与周期 1(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 1 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 2 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 3 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 4 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
然后
第 1 行第 0 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 1 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 2 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 3 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 4 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
然后
第 2 行第 0 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 1 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 2 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 3 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 4 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
我的目标是获取 Period 0 的第一行,然后将其转换为 1d 数组,然后获取 Period1 的第一行,然后将其转换为 1d 数组,依此类推..
二维数组如下;
double[][] myDistributions = new double[][]{
row0 {0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
row1 {0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
Period0 row2 {0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row0 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row1 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
Period1 row2 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row3 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row4 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row0 {0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period2 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
row4 {0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996},
row0 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period3 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}};
我找到了解决方案。
int numOfTopicsInEachPeriod=5;
double[][] myFirstRow=new double [numOfTopicsInEachPeriod][myDistributions[0].length];
double[][] mySecondRow=new double[numOfTopicsInEachPeriod][myDistributions[0].length];
int count =0;
int counter =0;
int temp=0;
for(int j =0;j<myDistributions.length-numOfTopicsInEachPeriod;j++){
int mod=j%numOfTopicsInEachPeriod;
int countOfTopics = j/numOfTopicsInEachPeriod;
count=0;
if(mod==0){
temp=1;
count=numOfTopicsInEachPeriod* countOfTopics;
}
if(temp==1&&mod!=0){count=numOfTopicsInEachPeriod* countOfTopics; }
for(int i=counter;i<numOfTopicsInEachPeriod;i++){
myFirstRow[i]=myDistributions[count];
mySecondRow[i]=myDistributions[count+5];
System.out.print("myFunction(Row["+j+"],Row ["+(count+5)+"]),"+"\t" );
count++;
}
System.out.println();
}
现在您已经在您的评论中提到了您所做的几乎所有事情,我们可以有点了解您在做什么试图在这里完成。坦率地说,没有任何不尊重,你的解释很糟糕。 :P
按照我收集的方式,您正在尝试从两个特定的二维 (2D) 双精度型数组中获取行,并从这些特定的双精度型数据行中检索散度值。如果你这样做有分歧,你当然没有透露类型,所以我将不得不假设它是 Kullback–Leibler Divergence。无论哪种方式,您都可以采用我在下面提供的代码并将其修改为调用您希望收集所需分歧的任何 Java 方法。
为了完成这项任务,我们需要一些特定的方法,其中最重要的方法当然是为我们提供分歧的方法。我在下面提供的方法就是这样做的,它是由 Univ 的 Charles Sutton 不久前编写的。马萨诸塞州阿默斯特计算机科学系的方法。该方法是 "MALLET" 的一部分(MAchine Learning for LanguagE Toolkit). Here is the Mr. Sutton's Divergence method 是开源的:
/**
* Returns the Kullback–Leibler (KL) Divergence, K(p1 || p2).
*
* The log is w.r.t. base 2. <p>
*
* *Note*: If any value in <tt>p2</tt> is <tt>0.0</tt> then the KL-divergence
* is <tt>infinite</tt>. Limin changes it to zero instead of infinite.
*
*/
public static double klDivergence(double[] p1, double[] p2) {
double log2 = Math.log(2);
double klDiv = 0.0;
for (int i = 0; i < p1.length; ++i) {
if (p1[i] == 0) { continue; }
if (p2[i] == 0.0) { continue; } // Limin
klDiv += p1[i] * Math.log( p1[i] / p2[i] );
}
return klDiv / log2; // moved this division out of the loop -DM
}
对于我们的下一个方法,我们需要一种方法来获取两个提供的二维双精度类型数组并从中提取每一行数据,以便从这两个特定行中检索差异,无论它们是什么。我在下面提供的方法 (kldFromDoubleArrays()) 执行此操作。它尽可能基础,因此很容易理解:
/**
* This method will take each row from the supplied 2D double type array1 and
* each row from the supplied 2D double type array2 and display the
* Kullback–Leibler Divergence for each of those rows of data processed.<br><br>
*
* Note: This method outputs its results into the Console Window.<br><br>
*
* Note: This method utilizes <b>Charles Sutton's</b> method named klDivergence() to
* acquire Kullback–Leibler Divergence values.<br><br>
*
* @param array1 (2D Double Type Array)<br>
*
* @param nameForArray1 (String) The string name to use for array1 for console
* display purposes.<br>
*
* @param array2 (2D Double Type Array)<br>
*
* @param nameForArray2 (String) The string name to use for array2 for console
* display purposes.
*/
private void kldFromDoubleArrays(double[][] array1, String nameForArray1,
double[][] array2, String nameForArray2) {
//Iterate through Rows of array1...
for (int i = 0; i < array1.length; i++) {
//Declare a 1D Array to hold current row from array1
double[] p0 = new double [array1[i].length];
//Iterate through Columns of current array1 Row...
for (int j = 0; j < array1[i].length; j++) {
//Place current array1 row into a 1D Array p0
p0[j] = array1[i][j];
//Iterate through Rows of array2...
for (int k = 0; k < array2.length; k++) {
//Declare a 1D Array to hold current row from array2
double[] p1 = new double[array2[k].length];
//Iterate through Columns of current array2 Row...
for (int l = 0; l < array2[k].length; l++) {
//Place current array2 row into a 1D Array p1
p1[l] = array2[k][l];
//Get the KL Divergence fpr p0 and p1 1D arrays
//and display it within the Console window
double kld = klDivergence(p0, p1);
//Display to Console
System.out.println("The Divergence between Row " + i +
" of " + nameForArray1 + " and Row " + k + " of " +
nameForArray2 + " is: --> " + kld);
}
}
}
}
}
您可以轻松修改此方法以完全满足您的需要。
根据您原来的 post,我假设您的二维数组与特定周期相关,因此 period0[][]、period1[][]、period2[][],最后期间 3[][]。因此,四个 2D double 类型数组的结构通常为:
double[][] period0 = {
{0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
{0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
{0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552}
};
double[][] period1 = {
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175}
};
double[][] period2 = {
{0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996}
};
double[][] period3 = {
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}
};
现在我们需要做的就是调用我们的 kldFromDoubleArrays() 方法来处理您想要的周期。在您的 post 中,您指定要处理的行是:
- period0 的每一行与 period1 的每一行;
- period1 的每一行与 period2 的每一行;
- period2 的每一行与 period3 的每一行;
所以,知道这一点我们将调用我们的方法三次:
//Create a Underline for Console window display.
String ul = String.join("", Collections.nCopies(100, "=")) + "\n";
//Period0 To Period1 Comparison:
kldFromDoubleArrays(period0, "Period 0", period1, "Period 1");
System.out.println(ul);
//Period1 To Period2 Comparison:
kldFromDoubleArrays(period1, "Period 1", period2, "Period 2");
System.out.println(ul);
//Period2 To Period3 Comparison:
kldFromDoubleArrays(period2, "Period 2", period3, "Period 3");
System.out.println(ul);
这是一些示例输出:
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
至此结束。我希望这对你有所帮助。祝你的项目好运。
你好,我想问一下关于二维数组比较的问题。 我在每行中都有以下带有双参数的二维数组。 我想比较前 5 行(即:Period 0)和 secont 5 行(即:Period1) 我想比较 比较将逐行作为一维数组我需要两个一维数组来相互比较..
例子;
周期 0(第 0 行)与周期 1(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 1 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 2 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 3 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 0 行第 4 行与第 1 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
然后
第 1 行第 0 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 1 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 2 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 3 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 1 行第 4 行与第 2 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
然后
第 2 行第 0 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 1 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 2 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 3 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
第 2 行第 4 行与第 3 行(第 0 行、第 1 行、第 2 行、第 3 行、第 4 行)
我的目标是获取 Period 0 的第一行,然后将其转换为 1d 数组,然后获取 Period1 的第一行,然后将其转换为 1d 数组,依此类推..
二维数组如下;
double[][] myDistributions = new double[][]{
row0 {0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
row1 {0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
Period0 row2 {0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row0 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row1 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
Period1 row2 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row3 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row4 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row0 {0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period2 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
row4 {0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996},
row0 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period3 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}};
我找到了解决方案。
int numOfTopicsInEachPeriod=5;
double[][] myFirstRow=new double [numOfTopicsInEachPeriod][myDistributions[0].length];
double[][] mySecondRow=new double[numOfTopicsInEachPeriod][myDistributions[0].length];
int count =0;
int counter =0;
int temp=0;
for(int j =0;j<myDistributions.length-numOfTopicsInEachPeriod;j++){
int mod=j%numOfTopicsInEachPeriod;
int countOfTopics = j/numOfTopicsInEachPeriod;
count=0;
if(mod==0){
temp=1;
count=numOfTopicsInEachPeriod* countOfTopics;
}
if(temp==1&&mod!=0){count=numOfTopicsInEachPeriod* countOfTopics; }
for(int i=counter;i<numOfTopicsInEachPeriod;i++){
myFirstRow[i]=myDistributions[count];
mySecondRow[i]=myDistributions[count+5];
System.out.print("myFunction(Row["+j+"],Row ["+(count+5)+"]),"+"\t" );
count++;
}
System.out.println();
}
现在您已经在您的评论中提到了您所做的几乎所有事情,我们可以有点了解您在做什么试图在这里完成。坦率地说,没有任何不尊重,你的解释很糟糕。 :P
按照我收集的方式,您正在尝试从两个特定的二维 (2D) 双精度型数组中获取行,并从这些特定的双精度型数据行中检索散度值。如果你这样做有分歧,你当然没有透露类型,所以我将不得不假设它是 Kullback–Leibler Divergence。无论哪种方式,您都可以采用我在下面提供的代码并将其修改为调用您希望收集所需分歧的任何 Java 方法。
为了完成这项任务,我们需要一些特定的方法,其中最重要的方法当然是为我们提供分歧的方法。我在下面提供的方法就是这样做的,它是由 Univ 的 Charles Sutton 不久前编写的。马萨诸塞州阿默斯特计算机科学系的方法。该方法是 "MALLET" 的一部分(MAchine Learning for LanguagE Toolkit). Here is the Mr. Sutton's Divergence method 是开源的:
/**
* Returns the Kullback–Leibler (KL) Divergence, K(p1 || p2).
*
* The log is w.r.t. base 2. <p>
*
* *Note*: If any value in <tt>p2</tt> is <tt>0.0</tt> then the KL-divergence
* is <tt>infinite</tt>. Limin changes it to zero instead of infinite.
*
*/
public static double klDivergence(double[] p1, double[] p2) {
double log2 = Math.log(2);
double klDiv = 0.0;
for (int i = 0; i < p1.length; ++i) {
if (p1[i] == 0) { continue; }
if (p2[i] == 0.0) { continue; } // Limin
klDiv += p1[i] * Math.log( p1[i] / p2[i] );
}
return klDiv / log2; // moved this division out of the loop -DM
}
对于我们的下一个方法,我们需要一种方法来获取两个提供的二维双精度类型数组并从中提取每一行数据,以便从这两个特定行中检索差异,无论它们是什么。我在下面提供的方法 (kldFromDoubleArrays()) 执行此操作。它尽可能基础,因此很容易理解:
/**
* This method will take each row from the supplied 2D double type array1 and
* each row from the supplied 2D double type array2 and display the
* Kullback–Leibler Divergence for each of those rows of data processed.<br><br>
*
* Note: This method outputs its results into the Console Window.<br><br>
*
* Note: This method utilizes <b>Charles Sutton's</b> method named klDivergence() to
* acquire Kullback–Leibler Divergence values.<br><br>
*
* @param array1 (2D Double Type Array)<br>
*
* @param nameForArray1 (String) The string name to use for array1 for console
* display purposes.<br>
*
* @param array2 (2D Double Type Array)<br>
*
* @param nameForArray2 (String) The string name to use for array2 for console
* display purposes.
*/
private void kldFromDoubleArrays(double[][] array1, String nameForArray1,
double[][] array2, String nameForArray2) {
//Iterate through Rows of array1...
for (int i = 0; i < array1.length; i++) {
//Declare a 1D Array to hold current row from array1
double[] p0 = new double [array1[i].length];
//Iterate through Columns of current array1 Row...
for (int j = 0; j < array1[i].length; j++) {
//Place current array1 row into a 1D Array p0
p0[j] = array1[i][j];
//Iterate through Rows of array2...
for (int k = 0; k < array2.length; k++) {
//Declare a 1D Array to hold current row from array2
double[] p1 = new double[array2[k].length];
//Iterate through Columns of current array2 Row...
for (int l = 0; l < array2[k].length; l++) {
//Place current array2 row into a 1D Array p1
p1[l] = array2[k][l];
//Get the KL Divergence fpr p0 and p1 1D arrays
//and display it within the Console window
double kld = klDivergence(p0, p1);
//Display to Console
System.out.println("The Divergence between Row " + i +
" of " + nameForArray1 + " and Row " + k + " of " +
nameForArray2 + " is: --> " + kld);
}
}
}
}
}
您可以轻松修改此方法以完全满足您的需要。
根据您原来的 post,我假设您的二维数组与特定周期相关,因此 period0[][]、period1[][]、period2[][],最后期间 3[][]。因此,四个 2D double 类型数组的结构通常为:
double[][] period0 = {
{0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
{0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
{0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552}
};
double[][] period1 = {
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175}
};
double[][] period2 = {
{0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996}
};
double[][] period3 = {
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}
};
现在我们需要做的就是调用我们的 kldFromDoubleArrays() 方法来处理您想要的周期。在您的 post 中,您指定要处理的行是:
- period0 的每一行与 period1 的每一行;
- period1 的每一行与 period2 的每一行;
- period2 的每一行与 period3 的每一行;
所以,知道这一点我们将调用我们的方法三次:
//Create a Underline for Console window display.
String ul = String.join("", Collections.nCopies(100, "=")) + "\n";
//Period0 To Period1 Comparison:
kldFromDoubleArrays(period0, "Period 0", period1, "Period 1");
System.out.println(ul);
//Period1 To Period2 Comparison:
kldFromDoubleArrays(period1, "Period 1", period2, "Period 2");
System.out.println(ul);
//Period2 To Period3 Comparison:
kldFromDoubleArrays(period2, "Period 2", period3, "Period 3");
System.out.println(ul);
这是一些示例输出:
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
至此结束。我希望这对你有所帮助。祝你的项目好运。