Java examples for java.lang:Math Matrix
Return an array of the means of each row in the 2D input matrix
/*//from w w w . j a v a 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 { /** * Return an array of the means of each row in the 2D input matrix * * @param input * @return */ public static double[] meansOfRows(double[][] input) { double[] theMeans = new double[input.length]; for (int i = 0; i < input.length; i++) { theMeans[i] = mean(input[i]); } return theMeans; } public static double mean(int[] input) { return sum(input) / (double) input.length; } public static double mean(double[] input) { return sum(input) / (double) input.length; } public static double mean(double[] input, int startIndex, int length) { return sum(input, startIndex, length) / (double) length; } public static double mean(double[][] input) { return sum(input) / (double) (input.length * input[0].length); } /** * Compute the mean along the given column * * @param input * @param column * @return */ public static double mean(double[][] input, int column) { return sum(input, column) / (double) input.length; } public static double sum(double[] input) { double total = 0; for (int i = 0; i < input.length; i++) { total += input[i]; } return total; } public static double sum(double[] input, int startIndex, int length) { double total = 0; for (int i = startIndex; i < startIndex + length; i++) { total += input[i]; } return total; } public static double sum(double[][] input) { double total = 0; for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[i].length; j++) { total += input[i][j]; } } return total; } public static double sum(double[][] input, int column) { double total = 0; for (int i = 0; i < input.length; i++) { total += input[i][column]; } return total; } public static int sum(int[] input) { int total = 0; for (int i = 0; i < input.length; i++) { total += input[i]; } return total; } public static int sum(int[][] input) { int total = 0; for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[i].length; j++) { total += input[i][j]; } } return total; } }