简体   繁体   English

将二维双精度数组转换为二维int数组

[英]convert a 2d double array to a 2d int array

I want to convert a 2d double array to another 2D array of integers. 我想将二维双精度数组转换为另一个二维整数数组。 The double array is filled with values lower than 1. What I want to do is create a new int[][]array and, using a if else statement set the values in the new array to 1 and 0. I have posted my attempt but I am not sure where to initialize my int array which is called intMatrix. double数组填充的值小于1。我要创建一个新的int [] [] array,并使用if else语句将新数组中的值设置为1和0。但是我不确定在哪里初始化我的称为intMatrix的int数组。

public int[][] readCorrMatrix(){

    String filename=ReadFile.getPath();
    double[][]rawDataMatrix=ReadMatrix.readDataMatrix(filename, ",");
    RealMatrix speaRealMatrix=Spearmans.calcSpearMatrix(rawDataMatrix);
    double[][]speaRealMatrixArray=speaRealMatrix.getData();
    int[][]intMatrix=null;
    for(int i=0; i<speaRealMatrixArray.length;i++){
        for(int y=0; y<speaRealMatrixArray[i].length;y++){

            intMatrix[i][y]=(int)speaRealMatrixArray[i][y];
            if(speaRealMatrixArray[i][y]<0.6)
            {
                intMatrix[i][y]=0;
            }
                else
                {
                    intMatrix[i][y]=1;
                }               
        }
    }
    return intMatrix;
}

Do you guys have any idea as to what I am doing wrong. 你们对我在做什么错有任何想法。 Thanks in advance, Jetnori. 在此先感谢Jetnori。

int[][]intMatrix= new int[speaRealMatrixArray.length][];
...
for(int i=0; i<speaRealMatrixArray.length;i++){
      intMatrix[i] = new new int[speaRealMatrixArray[i].length];
      for(int y=0; y<speaRealMatrixArray[i].length;y++){
...

Your if is checking the wrong array. 您的if正在检查错误的数组。

intMatrix[i][y]=(int)speaRealMatrixArray[i][y]; intMatrix [i] [y] =(int)speaRealMatrixArray [i] [y]; will be always 0 if we assume that the values in the given matrix are < 1. And in the next line you check its value which must be 0 anyhow. 如果我们假设给定矩阵中的值<1,则它将始终为0。在下一行中,您必须检查其值,该值必须始终为0。 Remove the assignment and check speaRealMatrixArray[i][y] < 0.6 删除分配并检查speaRealMatrixArray [i] [y] <0.6

Also you've got to initialize the int array correctly. 另外,您还必须正确初始化int数组。

Initialize intMatrix properly. 正确初始化intMatrix。

Also instead of 也代替

intMatrix[i][y] = (int) speaRealMatrixArray[i][y];
if (intMatrix[i][y] < 0.6) {
    intMatrix[i][y] = 0;
} else {
    intMatrix[i][y] = 1;
}  

try 尝试

if ( speaRealMatrixArray[i][y] < 0.6) {
    intMatrix[i][y] = 0;
} else {
    intMatrix[i][y] = 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM