有效地获取Java 2D数组的row.max和row.sum

时间:2017-06-26 11:07:08

标签: java matrix rowsum

在Java中,给定一个二维值的数组,其中dim为6000 * 6000,是否有一种查询行最大值和行总和的有效方法?

我使用数据结构double [] []和两层循环来获取行max和sum,但效率不高,因为此函数经常被调用。

double MinRowMax = Double.POSITIVE_INFINITY;
int num = 6000;
double[][] array2DDist = new double[num][num];
Random rand = new Random();

// initialising the array2DDist
for(int i=0;i<num;++i)
    for(int j=0;j<num;++j)
        array2DDist[i][j] = rand.nextDouble();

// get the row.max and row.sum
for(int i=0;i<num;++i) {
    double maxDist = Double.NEGATIVE_INFINITY;
    double sumDist = 0;
    for(int j=0;j<num;++j) {
        double dist = array2DDist[i][j];
        maxDist = Double.max(maxDist, dist);
        sumDist+=dist;
    }
    if(maxDist < MinRowMax) {
        MinRowMax = maxDist;
    }
}

是否有任何Java库可以提供更有效的解决方案?是否有任何类似于Python或R中的Matrix类的Java库?

谢谢!

3 个答案:

答案 0 :(得分:1)

计算数组的总和或数组中的最大值,您必须访问数组的每个元素。你不能加快速度。

但是,如果数组不会改变,并且您将需要多次数组的和和最大值,那么您可以计算它们一次然后查找它们。有两种方法:

  • 在开始时计算二维数组所有行的必需值,并将它们存储在查找表中。这是一个表单或渴望缓存。

  • 使用(比方说)HashMap<Integer, CacheEntry>(其中CacheEntry表示总和和最大值),然后使用此 lazily 缓存每行所需的值(由密钥索引)。

(或上述实施的一些变化。)

  

是否有任何Java库可以提供更有效的解决方案?是否有任何类似于Python或R中的Matrix类的Java库?

据我所知。当然,不是在标准的Java类库中。

但是,如果您使用 eager lazy 缓存,则不需要库...来解决此问题。

答案 1 :(得分:0)

我不知道使用Stream是否更有效但更短。这是一个使用4x4阵列的演示:

    double MinRowMax = Double.POSITIVE_INFINITY;
    int num = 4;
    double[][] array2DDist = new double[num][num];
    Random rand = new Random();

    // initializing the array2DDist
    for(int i=0;i<num;++i) {
        for(int j=0;j<num;++j) {
            array2DDist[i][j] = rand.nextDouble();
        }
    }

    // get the row.max and row.sum
    for(int row=0;row<num;++row) {

        double maxDist = Double.NEGATIVE_INFINITY;
        double sumDist = 0;

        for(int col=0;col<num;++col) {

            double dist = array2DDist[row][col];
            maxDist = Double.max(maxDist, dist);
            sumDist+=dist;
        }

        //System.out.println(Arrays.toString(array2DDist[row]));
        System.out.println("row sum - max " + sumDist +" - " + maxDist);
        System.out.println("row sum - max " + Arrays.stream(array2DDist[row]).parallel().sum()
                +" - " + Arrays.stream(array2DDist[row]).parallel() .max().getAsDouble());

        if(maxDist < MinRowMax) {
            MinRowMax = maxDist;
        }
    }

答案 2 :(得分:0)

// Programme to get sum of rows value and column values seprately.

    int[] colSum =new int[array[0].length];
    for (int i = 0; i < array.length; i++){   
        for (int j = 0; j < array[i].length; j++){                
            sum += array[i][j];
            colSum[j] += array[i][j];
        }
        System.out.println("Print the sum of rows =" + sum);
    }  
    for(int k=0;k<colSum.length;k++){
        System.out.println("Print the sum of columns =" + colSum[k]);
    } 


// Programme to get maximum in 2D array.

map<int, int> temp;
int currentMax= -999999,maxCount=0;
for(i=0; i< numberOflines ;i++)
{
        for(j=0;j< array[i].length;j++)
        {
            int newCount = ++temp[array[i][j]];
            if (maxCount < newCount) {
                 maxCount = newCount;
                 currentMax = array[i][j];
            }
        }
}