谁能告诉我如何使用Colt库从矩阵中删除列?
Neil Hainer
问问题
724 次
1 回答
1
没有明确的方法来删除一列,但是您可以创建一个原始矩阵的视图,其中包含除您想要删除的列之外的所有列。
/**
* Returns a view of the original matrix that contains all rows and all columns
* except for the specified column.
*
* The view is backed by the original matrix, that is, all changes to the
* returned matrix will be reflected by the original matrix.
*
* @param src The matrix to have a column "removed".
* @param colIdx The index of the column to be hidden
* ({@code 0 <= colIdx < src.columns()} .
* @return A view of the original matrix with column {@code colIdx} removed.
*/
public DoubleMatrix2D hideColumn(final DoubleMatrix2D src, final int colIdx) {
// create array of column indices to be preserved
final int[] keepColumns = new int[src.columns() - 1];
for (int i = 0; i < keepColumns.length; i++) {
keepColumns[i] = ((i < colIdx) ? i : i + 1);
}
return src.viewSelection(null /* keep ALL rows */, keepColumns);
}
于 2009-10-02T11:54:09.967 回答