2009-02-23 53 views

回答

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); 
} 
+1

感謝您的回答。問題是:如果我們只想保留新矩陣,我們能做什麼?我們可以使用`M = M.viewSelection(...)。copy()`? (看起來很安全,但在性能方面很差) – ThR37 2011-07-01 14:07:04