Works out the maximum value in the matrix in a given column - Java java.lang

Java examples for java.lang:Math Matrix

Description

Works out the maximum value in the matrix in a given column

Demo Code

/*/*from  w  ww.  ja  v  a 2  s  .  c om*/
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    public static double max(double[][][] matrix) {
        // double max = 0.0;
        double max = matrix[0][0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                for (int k = 0; k < matrix[i][j].length; k++) {
                    if (matrix[i][j][k] > max) {
                        max = matrix[i][j][k];
                    }
                }
            }
        }
        return max;
    }

    public static double max(double[][] matrix) {
        // double max = 0.0;
        double max = matrix[0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (Double.isNaN(max) || (matrix[i][j] > max)) {
                    max = matrix[i][j];
                }
            }
        }
        return max;
    }

    public static int max(int[][] matrix) {
        // int max = 0;
        int max = matrix[0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] > max) {
                    max = matrix[i][j];
                }
            }
        }
        return max;
    }

    public static double max(double[] array) {
        return maxStartFromIndex(array, 0);
    }

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

    /**
     * Works out the maximum value in the matrix in a given column 
     * 
     * @param matrix
     * @param column
     * @return
     */
    public static double max(double[][] matrix, int column) {
        // double max = 0.0;
        // Allow ArrayIndexOutOfBoundsException if matrix is size 0
        double max = matrix[0][column];
        for (int i = 1; i < matrix.length; i++) {
            if (Double.isNaN(max) || (matrix[i][column] > max)) {
                max = matrix[i][column];
            }
        }
        return max;
    }

    /**
     * Works out the maximum value in the matrix in a given column 
     * 
     * @param matrix
     * @param column
     * @return
     */
    public static int max(int[][] matrix, int column) {
        // double max = 0.0;
        // Allow ArrayIndexOutOfBoundsException if matrix is size 0
        int max = matrix[0][column];
        for (int i = 1; i < matrix.length; i++) {
            if (matrix[i][column] > max) {
                max = matrix[i][column];
            }
        }
        return max;
    }

    public static double maxStartFromIndex(double[] array,
            int startFromIndex) {
        // double max = 0.0;
        double max = array[startFromIndex];
        for (int i = startFromIndex; i < array.length; i++) {
            // TODO Check where we used this and if it's still
            //  the approach we want to take
            if (Double.isNaN(max) || (array[i] > max)) {
                max = array[i];
            }
        }
        return max;
    }
}

Related Tutorials