我在想是否有一种有效的方法来查找稀疏矩阵中的最大/最小系数。
似乎sparse矩阵没有实现minCoeff()/maxCoeff()函数,这有点奇怪。
我在这里找到了答案,但我无法理解。
似乎sparse矩阵没有实现minCoeff()/maxCoeff()函数,这有点奇怪。
我在这里找到了答案,但我无法理解。
using Tmp = typename remove_cv_ref<decltype(matrix)>::type;
if constexpr(std::is_base_of<Eigen::SparseMatrixBase<Tmp>, Tmp>::value)
max = Eigen::Map<const Vector>(matrix.valuePtr(), matrix.size()).maxCoeff();
else
max = matrix.maxCoeff();
编辑: 这是我的尝试,我不确定效率如何。
typedef Eigen::SparseMatrix<int, Eigen::RowMajor> SRI;
int maxCoeff(const SparseMatrix<int, RowMajor> &A)
{
size_t row = A.rows();
size_t col = A.cols();
int max_value = -10000000;
for (size_t k = 0; k < row; k++)
{
for (SRI::InnerIterator it(A, k); it; ++it)
{
if (it.value() > max_value)
max_value = it.value();
}
}
return max_value;
}
error: no matching function for call to ‘sparseMax(Eigen::SparseMatrix<int, 1>&)’ auto max = sparseMax(mat);
template argument ‘1’ does not match ‘0’ auto max = sparseMax(mat);
- AbolfazlT sparseMax(SparseMatrix <T,RowMajor>&mat)
。为了拥有一个接受“RowMajor”和“ColMajor”存储的函数而概括答案是不必要的,因为成员函数.coeff()
似乎正好做到了这一点。 - RHertel