From 62069dab1655a7e0d3216632afe0be0b8a91a8e3 Mon Sep 17 00:00:00 2001 From: jlizier Date: Thu, 23 Mar 2017 14:31:38 +1100 Subject: [PATCH] Added several utilities for selecting columns to MatrixUtils --- .../infodynamics/utils/MatrixUtils.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/java/source/infodynamics/utils/MatrixUtils.java b/java/source/infodynamics/utils/MatrixUtils.java index 7203b7a..8c5b799 100755 --- a/java/source/infodynamics/utils/MatrixUtils.java +++ b/java/source/infodynamics/utils/MatrixUtils.java @@ -1299,6 +1299,14 @@ public class MatrixUtils { return returnData; } + public static boolean[] selectColumn(boolean matrix[][], int columnNo) { + boolean[] column = new boolean[matrix.length]; + for (int r = 0; r < matrix.length; r++) { + column[r] = matrix[r][columnNo]; + } + return column; + } + public static int[] selectColumn(int matrix[][], int columnNo) { int[] column = new int[matrix.length]; for (int r = 0; r < matrix.length; r++) { @@ -1342,6 +1350,25 @@ public class MatrixUtils { return column; } + /** + * Extract the required columns from the matrix + * + * @param matrix + * @param fromCol + * @param cols + * @return + */ + public static double[][] selectColumns(double matrix[][], + int fromCol, int cols) { + double[][] data = new double[matrix.length][cols]; + for (int r = 0; r < matrix.length; r++) { + for (int cIndex = 0; cIndex < cols; cIndex++) { + data[r][cIndex] = matrix[r][cIndex + fromCol]; + } + } + return data; + } + /** * Extract the required columns from the matrix * @@ -1435,6 +1462,27 @@ public class MatrixUtils { return data; } + /** + * Extract the required rows and columns from the matrix + * + * @param matrix 2D data array + * @param fromRow index of the first row to return + * @param rows number of rows (including the first) to return + * @param fromCol index of the first column to return + * @param cols number of columns (including the first) to return + * @return a 2D data array of the selected rows and columns + */ + public static double[][] selectRowsAndColumns(double matrix[][], + int fromRow, int rows, int fromCol, int cols) { + double[][] data = new double[rows][cols]; + for (int rIndex = 0; rIndex < rows; rIndex++) { + for (int cIndex = 0; cIndex < cols; cIndex++) { + data[rIndex][cIndex] = matrix[rIndex + fromRow][cIndex + fromCol]; + } + } + return data; + } + public static double[][] selectFirstTwoDimenions(double[][][][] matrix, int d2, int d3) { double[][] newMatrix = new double[matrix.length][]; for (int i = 0; i < matrix.length; i++) {