Find the standard deviation of an array. - Java java.lang

Java examples for java.lang:Math Array Function

Description

Find the standard deviation of an array.

Demo Code


//package com.java2s;

public class Main {
    /**/*  w  w  w.  j  av  a2 s.co m*/
     * Find the standard deviation of an array.
     * @param vals The array of values to find the standard deviation for.
     * @param arrayAvg The average of the array of values.
     * @return The standard deviation of the array.
     */
    public static double StdDevOfArray(double[] vals, double arrayAvg) {
        double runTotal = 0;
        int len = vals.length;
        for (int i = 0; i < len; i++) {
            runTotal += Math.pow(arrayAvg - vals[i], 2);
        }
        if (len > 1) {
            runTotal /= len;
            runTotal = Math.sqrt(runTotal);
        }
        return runTotal;
    }
}

Related Tutorials