Java mean mean(float[] arr)

Here you can find the source of mean(float[] arr)

Description

Find the mean of a single dimensional float array.

License

Open Source License

Parameter

Parameter Description
arr a parameter

Return

the mean

Declaration

public static float mean(float[] arr) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww w.  jav a 2s  .  c  om
     * Find the mean of a single dimensional float array. returns 0 if the array
     * is empty.
     * 
     * @param arr
     * @return the mean
     */
    public static float mean(float[] arr) {
        if (arr.length == 0) {
            return 0;
        }
        int count = 1;
        float mean = arr[0];
        for (int i = 1; i < arr.length; i++) {
            count++;
            mean = mean + (arr[i] - mean) / count;
        }
        return mean;
    }

    /**
     * Calculate the mean of a two dimensional float array. returns 0 if the
     * array is empty.
     * 
     * @param arr
     * @return the mean
     */
    public static float mean(float[][] arr) {
        if (arr.length == 0) {
            return 0;
        }
        int firstRowIndex = 0;
        while (arr[firstRowIndex].length == 0)
            firstRowIndex++;
        int firstColIndex = 1;

        int count = 1;
        float mean = arr[firstRowIndex][0];

        for (int i = firstRowIndex; i < arr.length; i++) {
            for (int j = firstColIndex; j < arr[i].length; j++) {
                count++;
                mean = mean + (arr[i][j] - mean) / count;
            }
            firstColIndex = 0;
        }

        return mean;
    }
}

Related

  1. mean(final double[] vec)
  2. mean(final int[] scores)
  3. mean(final int[] values)
  4. mean(float a, float b)
  5. mean(float[] a, int off, int length)
  6. mean(float[] array)
  7. mean(float[] xs)
  8. mean(int low, int high)
  9. mean(int N, int D, double dat[][], double mu[])