稀疏特征矩阵子集列子集、稀疏、矩阵、特征

2023-09-03 09:55:20 作者:嫌我你滚 i

我想取一些稀疏矩阵的子集列(列为主) 据我所知,在Eigen中有indexing个东西。 但我不能为稀疏矩阵调用它:

Eigen::SparseMatrix<double> m;
std::vector<int> indices = {1, 5, 3, 6};
// error: type 'Eigen::SparseMatrix<double>' does not provide a call operator
m(Eigen::all, indices); 

是否有解决办法?

更新1 已明确指定列可以按任意顺序排列。

推荐答案

SparseMatrix实际上不提供任何operator(),因此无法执行此操作。

自学MATLAB 五 稀疏矩阵

编辑:以下内容针对的是旧版本的问题。

如果您的实际用例也具有要访问的列相邻的属性,则可以改用

SparseMatrix<double,ColMajor> m(5,5);
int j = 1;
int number_of_columns=2;
m.middleCols(j,number_of_columns) = ...;
还有m.leftCols(number_of_columns)m.rightCols(number_of_columns) 它们甚至是可写的,因为矩阵是以列为主的。

所有其他块表达式都已定义,但为只读,请参阅the corresponding Sparse Matrix documentation。

编辑:要回答更新的问题: 我猜你将无法避免抄袭。通过复制可以这样完成(未测试)

Eigen::SparseMatrix<double> m;
std::vector<int> indices = {1, 5, 3, 6};
Eigen::SparseMatrix<double> column_subset(m.rows(),indices.size());
for(int j =0;j!=column_subset.cols();++j){
column_subset.col(j)=m.col(indices[j]);
}