Computes the mean value of a matrix of integers. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Computes the mean value of a matrix of integers.

Demo Code


//package com.java2s;

public class Main {
    /**//w  w  w. j a va 2 s . c  o  m
     * Computes the mean value of a matrix of integers.
     *
     * @param matrix   A matrix of integers whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(int[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of doubles.
     *
     * @param matrix   Matrix of doubles whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(double[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of longs.
     *
     * @param matrix   Matrix of longs whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(long[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of floats.
     *
     * @param matrix   Matrix of floats whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static float mean(float[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        float mean = (float) (sum / (width * height));

        return mean;
    }

    /**
     * Computes the mean value of a matrix of bytes.
     *
     * @param matrix   Matrix of bytes whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static float mean(byte[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        float mean = (float) (sum / (width * height));

        return mean;
    }

    /**
     * Computes the mean value of an array of integers.
     *
     * @param array   Array of integers whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(int[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of doubles.
     *
     * @param array   Array of doubles whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(double[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of longs.
     *
     * @param array   Array of longs whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(long[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of floats.
     *
     * @param array   Array of floats whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(float[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of bytes.
     *
     * @param array   Array of bytes whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(byte[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }
}

Related Tutorials