Compute the standard deviations along each column - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Compute the standard deviations along each column

Demo Code

/*/*from w  w w. java 2  s .  co  m*/
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    /**
     * Compute the standard deviations along each column
     * 
     * @param matrix
     * @param means
     * @return
     */
    public static double[] stdDevs(double[][] matrix, double[] means) {
        double[] sumSqs = new double[means.length];
        for (int m = 0; m < matrix.length; m++) {
            for (int c = 0; c < matrix[m].length; c++) {
                sumSqs[c] += (matrix[m][c] - means[c])
                        * (matrix[m][c] - means[c]);
            }
        }
        double[] stds = new double[means.length];
        for (int c = 0; c < stds.length; c++) {
            stds[c] = sumSqs[c] / (double) (matrix.length - 1);
            stds[c] = Math.sqrt(stds[c]);
        }
        return stds;
    }
}

Related Tutorials