Here you can find the source of mean(float[] arr)
Parameter | Description |
---|---|
arr | a parameter |
public static float mean(float[] arr)
//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; } }