Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

armadillo c++: Efficient and concise way to multiply every row of a matrix by a vector?

I am wondering if there is an efficient and concise way to do an element-wise multiplication of every row (or column) of an Armadillo C++ matrix by a vector. The row (or column) and vector are the same size.

For example, IF fmat::each_row() (and/or each_col()) could be used as an rvalue, I'd want something like this to compile (currently it won't compile):

#include <armadillo>

int main()
{
    using namespace arma;

    fmat m(20, 10);
    fvec v(10); // a column vector

    m.each_row() % v.t(); // Currently a compiler error.

    return 0;
}
like image 213
Vince Avatar asked Feb 19 '14 18:02

Vince


1 Answers

From Armadillo version 5.6 onwards the .each_col() and .each_row() methods were expanded to handle out-of-place operations. Hence your suggested approach

m.each_row() % v.t();

should compile, see http://arma.sourceforge.net/docs.html#each_colrow.

like image 186
Svaberg Avatar answered Nov 01 '22 13:11

Svaberg