找到稀疏矩阵(特征)最大值的有效方法
Efficient way to find maximum value of Sparse matrix (Eigen)
我想知道是否有可能以有效的方式在稀疏矩阵中找到最大/最小系数。
似乎 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;
}
这些函数并不容易获得,因为是否应考虑隐式零可能不明确。例如,如果所有非零都是负数,maxCoeff
是否应返回0
?
如果您只想考虑显式存储的元素,并且您的稀疏矩阵处于压缩模式,那么您可以编写:
auto max = matrix.coeffs().maxCoeff();
coeff方法等同于RHertel的答案。
尝试使用此模板化函数来获取Eigen::SparseMatrix
的最大非零值:
template <class T>
T sparseMax (SparseMatrix<T>& mat) {
return Map<Matrix<T, Dynamic, 1> >(mat.valuePtr(), mat.nonZeros()).maxCoeff();
}
这里的原理在于将稀疏矩阵的非零元素映射到一个向量(一维Eigen::Matrix
),然后用.maxCoeff()
从中提取最大值。这应该是确定矩阵中最大值的非常有效的方法。 请注意,需要压缩矩阵才能使此方法正常工作。
在代码中,函数sparseMax()
可以像这样调用:
if (!mat.isCompressed()) mat.makeCompressed();
auto max = sparseMax(mat);
其中mat
是Eigen::SparseMatrix
.
因此,链接中的答案几乎是正确的,但.size()
应该替换为.nonZeros()
,并且它无法确保矩阵被压缩。如果您知道您正在处理稀疏矩阵,则不需要链接答案中提到的if/else
构造。
编辑
上述解决方案适用于具有默认列主存储顺序的稀疏矩阵。在RowMajor存储的情况下,可以将模板更改为
template <class T>
T sparseMax_RM (SparseMatrix<T,RowMajor>& mat) {
return Map<Matrix<T, Dynamic, 1> >(mat.valuePtr(), mat.nonZeros()).maxCoeff();
}
更健壮的方法是改用成员函数.coeffs()
,如@ggael所指出的。.coeffs()
函数适用于两种类型的压缩 SparseMatrix 类,即 RowMajor 和 ColMajor 存储顺序。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- C++不同的最小最大值实现
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 找到稀疏矩阵(特征)最大值的有效方法
- 从向量中提取最小值、最大值和中位数的最有效方法是什么
- 如何在大数组中最有效地增加指定范围内的值,然后找到最大值
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 有效地找到最小值和最大值