0

Coltライブラリを使用してマトリックスから列を削除する方法を教えてください。

4

1 に答える 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 に答える