Question

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;
}

No correct solution

OTHER TIPS

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.

It looks like you're using the wrong operator. According to the documentation for .each_row() and .each_col(), you need to specify an in-place operation (such as +=, -=, /=, %=). In other words, instead of %, use %=, as below:

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

m.each_row() %= v.t();   // right

Apart from the in-place operations, the only other allowed operation for .each_row() and .each_col() is "=" by itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top