简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM