简体   繁体   中英

calculate the minimum value for each column in 2D array

I have a 2D array , iam trying to calculate the minimum value for each column and put the result in the result array.

the code bellow is calculating the minimum value for each row , how can i get the min value for each column.

        import java.util.*;

         class Test20 {

        public static void main ( String [] args) {


            int[][] array = {{6,3,9},
                             {0,8,2},
                             {3,7,5}};



           Test20 test = new Test20();

        System.out.print(Arrays.toString(test.mincol(array)));
      }

     public static int[] mincol (int[][] n) {
    int[] result = new int[n.length];

    for (int i = 0; i < n.length; i++) {

        int min = n[0][i];

        for (int j = 0; j < n[0].length; j++) {

            if (n[j][i] < min) {
                min = n[j][i];
            }
        }
        result[i] = min;
    }
    return result;
     }
   }

Just change the loop the following way:

min = 0;
for(int i=0;i<n.length;i++){
     for(int j=0;j<n[0].length;j++){
        if(n[j][i]<n[j][min]){
        min=j;
}
result[i]=n[min][i];
}

Be aware that you instantiate your result array by the length of the first dimension in your array but later use the n[][] param for looping and access the length of the second dimension in your loop.

If your two dim array is for example 4x5, this will cause ArrayOutOfBoundsExceptions.

Your for loop looks ok. Check the code below I fixed some minor issues.

Based on your code replace Class code with below:

public class Test {
public static void main(String[] args) {
    int[][]array={{6,1,9}, {0,1,2}, {3,7,5}};
    int[] test;

    test = minrow(array);
    for(int i=0; i<test.length; i++){
        System.out.println(test[i]);
    }
}
public static int[] minrow(int[][] n){

    int [] result = new int[n.length];
    int min;

    for(int i=0;i<n.length;i++){
        min=0;
        for(int j=0;j<n[i].length;j++){
            if(n[i][j]<n[i][min]){
                min=j;
            }
        }
        result[i]=n[i][min];
    }
    return result;
}
}

You only need to do the same thing but inverting the variables

for(int i=0;i<n.length;i++){
   for(int j=0;j<n[0].length;j++){
       if(n[j][i]<n[min][j]){
           min=i;
       }
       result[j]=n[min][j];
    }

}

If your code is correct just change:

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

with

if(n[i][j]<n[result[i]][j]){
            result[i]=i;
}

finally

for(int i=0;i<n.length;i++) result[i]=n[result[i][j];

you don't need min . But change

 int [] result = new int[n.length];

to

int [] result = new int[n[0].length];

How about you transpose your two dimensional array like:

     public static int[][] transpose (int[][] original) {
        int[][] array = new int[original.length][];
        // transpose
        if (original.length > 0) {
            for (int i = 0; i < original[0].length; i++) {
                array[i] = new int[original[i].length];
                for (int j = 0; j < original.length; j++) {
                    array[i][j] = original[j][i];
                }
            }
        }
        return array;
    }

and then call it as:

System.out.print(Arrays.toString(test.minrow(transpose(array))));

Or, if you want to go without transpose, this is how you can do:

    public static int[] mincol (int[][] n) {
        int[] result = new int[n.length];
        for (int i = 0; i < n.length; i++) {
            int min = n[0][i];
            for (int j = 0; j < n[0].length; j++) {
                if (n[j][i] < min) {
                    min = n[j][i];
                }
            }
            result[i] = min;
        }
        return result;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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