简体   繁体   English

如何获得二维数组中的最小值

[英]how to get minimum in 2d array java

I need help, please! 我需要帮助! I am building up the following code to extract the minimum value of each column of the distance 我正在构建以下代码以提取距离的每一列的最小值

I have tried to compute the code but to no avail 我试图计算代码,但无济于事

public static void main(String[] args) {
    int data[] = {2, 4, -10, 12, 3, 20, 30, 11};//,25,17,23};    // initial data
    int noofclusters = 3;
    int centroid[][] = new int[][]{
            {0, 0, 0},
            {2, 4, 30}
    };
    getCentroid(data, noofclusters, centroid);
}

public static int[][] getCentroid(int[] data, int noofclusters, int[][] centroid) {

    int distance[][] = new int[noofclusters][data.length];
    int cluster[] = new int[data.length];
    int clusternodecount[] = new int[noofclusters];


    centroid[0] = centroid[1];
    centroid[1] = new int[]{0, 0, 0};
    System.out.println("========== Starting to get new centroid =========");

    for (int i = 0; i < noofclusters; i++) {
        for (int j = 0; j < data.length; j++) {
            //System.out.println(distance[i][j]+"("+i+","+j+")="+data[j]+"("+j+")-"+centroid[0][i]+"="+(data[j]-centroid[0][i]));
            distance[i][j] = Math.abs(data[j] - centroid[0][i]);
            System.out.print(distance[i][j] + " ,");
        }
        System.out.println();
    }

    int[] result = new int[distance.length];
        for (int i = 0; i < distance.length; i++) {
            //int min = distance;
            int min = distance[i][0];
            for (int j = 0; j < distance[0].length; j++) {

                if (distance[i][j] < min) {
                    min = distance[i][j];
                }

                result[j] = min;
                System.out.println(result[j] + ", ");
            }

        }
        return result;
    }
}

The result of the computation for distance gives 距离的计算结果给出

 row 1: 0 ,2 ,12 ,10, 1 ,18 ,28 ,9 row 2: 2 ,0 ,14 ,8 , 1 ,16 ,26 ,7 row 3: 28,26,40 ,18, 27 ,10 ,0 ,19 

I want to go through each column to get the minimum value 我想遍历每一列以获得最小值

0 0 12 8 1 10 0 7

Thanks for your help in advance 谢谢您的帮助

To get the minimum value in each column, first, you need to iterate the column in the outer loop. 要获得每列中的最小值,首先,需要在外循环中迭代该列。 By doing so, we can access the matrix colunm-wise. 这样,我们可以按色访问矩阵。

Assign the first value of each column to a variable. 将每列的第一个值分配给一个变量。 Then iterate through the rows in the inner loop. 然后迭代内部循环中的行。

Check if the current value is less than the minimum value. 检查当前值是否小于最小值。 If so, assign the smallest value to minimum. 如果是这样,则将最小值分配给最小值。

When we use an array, we get an array of minimum values of the column. 当我们使用数组时,我们得到列的最小值的数组。

To obtain the minimun value in each row, swap the loops and use min[i] . 要获得每一行的min[i] ,请交换循环并使用min[i]

Below is an example code: 下面是一个示例代码:

int []min = new int[column_lenght];
for(int j = 0; j < column_length; j++) {
    min[j] = array[i][j];
    for(int i = 0; i < row_length; i++) {
        if(array[i][j] < min[j]) {
            min[j] = array[i][j];
        }
    }
}

The min[] will contain the minimum value of each column. min[]将包含每列的最小值。

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

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