This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Generic Code to Iterate Over Dense Matrix?

Tags: None
(comma "," separated)
jbauer
Registered Member
Posts
25
Karma
0
If I have a templated function that accepts a parameter of Eigen::DenseBase<Derived>& matrix, is there a way to iterate over the entries of the DenseBase matrix in a way that respects the storage order? I see an innerSize and an outerSize method to get the sizes of each, but no way to actually get the values, e.g., I would like to do something like:

Code: Select all
for( int outer = 0; outer < matrix.outerSize(); ++outer )
{
  for( int inner = 0; inner < matrix.innerSize(); ++inner )
  {
    matrix.getValueAtOuterInner( outer, inner ) = generateSomeValue();
  }
}


Does some equivalent of getValueAtOuterInner exist, or should I perform this iteration in some other fashion?

Thank you!

... edited to fixe typo
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
here it is: mat.coeffByOuterInner(o,i)
jbauer
Registered Member
Posts
25
Karma
0
Thank you! Is there a non-const version? I suppose I can use
Code: Select all
matrix(matrix.rowIndexByOuterInner(o,i ),matrix.colIndexByOuterInner(o,i))

but that feels a bit verbose.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, coeffRefByOuterInner. They are the respective versions of the .coeff() and .coeffRef() methods.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]