fast way to multiply each element of a vector by all elements of a different vector of different length

32 views Asked by At

I want a matrix which is the result of the multiplication of each element in a vector (rowvec) by all elements in another vector (colvec), of different size. Here's a sample code of what I want to achieve but I'm looking for a faster way to do it.

#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
arma::mat myMultiplication() {
  arma::colvec cx = {0, 1, 2, 3, 4};
  arma::rowvec rx = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  arma::mat x(cx.size(), rx.size());
  for (unsigned int i = 0; i < cx.size(); i++) {
    x.row(i) = rx;
  }
  return cx % x.each_col();
}

0

There are 0 answers