I'm looking to get the row/column indices from a dense matrix which meet a condition. In my case the result is likely to be very sparse. For example, with matrix
1 5 2
7 6 3
2 3 8
I'd like to get the indicies where the coeff is greater than 4, or
(0,1), (1,0), (1,1), (2,2)
My initial thoughts include using either select or coefficientwise operations to build a bool/int matrix
0 1 0
1 1 0
0 0 1
and then convert it to a sparse matrix
(0,1,1), (1,0,1), (1,1,1), (2,2,1)
then remove the coeff values
(0,1), (1,0), (1,1), (2,2)
However, that requires two passes over matrices the size of the original matrix, which may be very large.
Alternatively a naive double loop over the original matrix akin to pseudocode
for (int i; i < mat.cols(); i++) {
for (int j; j < mat.rows(); j++) {
if(cond(mat(j, i))) {
add_index(i, j, index_list)
}
}
}
but that only takes the compilers optimizations and none of Eigen's optimizations/vectorizations.
Is there a more efficient way that I am missing? In my case the conditions are simple comparisons.
Thanks for any help
See Question&Answers more detail:os