Here you can find the source of mean(double[] a, int from, int to)
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 |
public static final double mean(double[] a, int from, int to)
//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); } }