Java mean mean(double[] a, int from, int to)

Here you can find the source of mean(double[] a, int from, int to)

Description

Computes the mean value in the specified range of an array.

License

Apache License

Parameter

Parameter Description
a input array
from initial index of the range to compute the mean, inclusive
to final index of the range to compute the mean, exclusive

Return

mean value in the specified range of input array

Declaration

public static final double mean(double[] a, int from, int to) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .  jav  a2s.c  o m*/
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Computes the mean value in the specified range of an array.   No error
     * checking is performed on range limits; if the values are negative or
     * outside the range of the array, a runtime exception may be thrown.
     *
     * @param a    input array
     * @param from initial index of the range to compute the mean, inclusive
     * @param to   final index of the range to compute the mean, exclusive
     * @return   mean value in the specified range of input array
     */
    public static final double mean(double[] a, int from, int to) {
        double sum = 0.0;
        for (int i = from; i < to; i++) {
            sum += a[i];
        }
        return sum / (double) (to - from);
    }

    /**
     * Computes the mean value of an array.
     *
     * @param a   input array
     * @return   mean value of input array
     */
    public static final double mean(double[] a) {
        return mean(a, 0, a.length);
    }

    /**
     * Computes the mean value in the specified range of an array.   No error
     * checking is performed on range limits; if the values are negative or
     * outside the range of the array, a runtime exception may be thrown.
     *
     * @param a    input array
     * @param from initial index of the range to compute the mean, inclusive
     * @param to   final index of the range to compute the mean, exclusive
     * @return mean value in the specified range of input array
     */
    public static final float mean(float[] a, int from, int to) {
        double sum = 0.0;
        for (int i = from; i < to; i++) {
            sum += a[i];
        }
        return (float) (sum / (to - from));
    }

    /**
     * Computes the mean value of an array.
     *
     * @param a   input array
     * @return mean value of input array
     */
    public static final float mean(float[] a) {
        return mean(a, 0, a.length);
    }
}

Related

  1. mean(double[] a)
  2. mean(double[] a)
  3. mean(double[] a)
  4. mean(double[] a, double[] b)
  5. mean(double[] a, double[] b)
  6. mean(double[] a, int n)
  7. mean(double[] aArray)
  8. mean(double[] an)
  9. mean(double[] arr)