14

プロジェクトに Eigen ライブラリを使用しています。Eigen の特定の行列から特定の行または列を削除する方法を探しています。私は成功していません。

MatrixXd A = X1 X2 X3 X4
             Y1 Y2 Y3 Y4
             Z1 Z2 Z3 Z4
             A1 A2 A3 A4
MatrixXd Atransform = X1 X2 X4
                      Y1 Y2 Y4
                      Z1 Z2 Z4
                      A1 A2 A4
enter code here

行列全体を反復するか、行列 A でブロック操作を使用する以外。それを簡単に行う方法はありますか。

4

7 に答える 7

17

ブロック関数を使用すると、少しすっきりします。

void removeRow(Eigen::MatrixXd& matrix, unsigned int rowToRemove)
{
    unsigned int numRows = matrix.rows()-1;
    unsigned int numCols = matrix.cols();

    if( rowToRemove < numRows )
        matrix.block(rowToRemove,0,numRows-rowToRemove,numCols) = matrix.block(rowToRemove+1,0,numRows-rowToRemove,numCols);

    matrix.conservativeResize(numRows,numCols);
}

void removeColumn(Eigen::MatrixXd& matrix, unsigned int colToRemove)
{
    unsigned int numRows = matrix.rows();
    unsigned int numCols = matrix.cols()-1;

    if( colToRemove < numCols )
        matrix.block(0,colToRemove,numRows,numCols-colToRemove) = matrix.block(0,colToRemove+1,numRows,numCols-colToRemove);

    matrix.conservativeResize(numRows,numCols);
}
于 2014-01-11T21:19:08.713 に答える
1

次の静的バージョンは、特定の用途に適している場合があります (また、Eigen のコンパイル時の効率の精神に沿ったものです)。この場合、行のない新しいマトリックスを作成します。を使用して、列に対して同様の関数を構築できます。.leftCols() .rightCols()

template<typename T>
inline constexpr auto removeRow(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& matrix, const int& rowNum)
{
    return (Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(matrix.rows() - 1, matrix.cols())
        << static_cast<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>(matrix.topRows(rowNum - 1)),
        static_cast<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>(matrix.bottomRows(matrix.rows() - rowNum))).finished();
}

楽しみ!

于 2017-11-23T16:05:03.473 に答える
-2

私はC ++を初めて使用しますが、このコードは5月のアプリケーションで機能します。

完全な動的マトリックスに対してのみ機能しますが、適応させることができます。

誰かがより良い方法を持っている場合は、私が本当に学びたいと思っていることを教えてください.

template<typename ScalarType>
void MatrixXdRemoveCol(Eigen::Matrix<ScalarType,-1,-1,0,-1,-1> *mat, int colindex)
{
    Eigen::Matrix<ScalarType,-1,-1,0,-1,-1> *auxmat = new Eigen::Matrix<ScalarType,-1,-1,0,-1,-1>;

    *auxmat = *mat;

    mat->resize(mat->rows(),mat->cols()-1);

    int rightColsSize = auxmat->cols()-colindex-1;

    mat->leftCols(colindex) = auxmat->leftCols(colindex);
    mat->rightCols(rightColsSize) = auxmat->rightCols(rightColsSize);
}

template<typename ScalarType>
void MatrixXdRemoveCols(Eigen::Matrix<ScalarType,-1,-1,0,-1,-1> *mat, std::vector<int>* cols)
{
    for(auto iter = cols->rbegin();iter != cols->rend();iter++)
        MatrixXdRemoveCol<ScalarType>(mat,*iter);
}

template<typename ScalarType>
void MatrixXdRemoveRow(Eigen::Matrix<ScalarType,-1,-1,0,-1,-1> *mat, int rowindex)
{
    Eigen::Matrix<ScalarType,-1,-1,0,-1,-1> *auxmat = new Eigen::Matrix<ScalarType,-1,-1,0,-1,-1>;

    *auxmat = *mat;

    mat->resize(mat->rows()-1,mat->cols());

    int BottomRowsSize = auxmat->rows()-rowindex-1;

    mat->topRows(rowindex) = auxmat->topRows(rowindex);
    mat->bottomRows(BottomRowsSize) = auxmat->bottomRows(BottomRowsSize);
}
于 2013-01-11T06:25:45.847 に答える