Added several utilities for selecting columns to MatrixUtils

This commit is contained in:
jlizier 2017-03-23 14:31:38 +11:00
parent bf3f26cf18
commit 62069dab16
1 changed files with 48 additions and 0 deletions

View File

@ -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++) {