犰狳C++中的元素向量或矩阵乘法

element wise vector or matrix multiplication in Armadillo C++

本文关键字:向量 C++ 元素 犰狳      更新时间:2023-10-16
#include<iostream>
#include<armadillo>
using namespace std;
using namespace arma;
int main()
   {    
      vec x = (1.0/5) * ones<vec>(N); //x is N sized uniformly distributed vector 
      vec xold(5); 
      mat v = randu<mat>(3,3);
      mat b =randu<mat>(3,3);
     mat c =  v .* b; //element-wise matrix multiplication
     xold = x .* x; // element-wise vector multiplication
 }
 //----------------------------this is the error message --------------------------------
/*
  In function ‘int main()’:
  SimilarityMatrix.cpp:182:17: error: ‘b’ cannot be used as a member pointer, since it is of      type ‘arma::mat {aka arma::Mat<double>}’
mat c =  (v.*b);
             ^

SimilarityMatrix.cpp:183:14:错误:"x"不能用作成员指针,因为它的类型为"arma::vec {aka arma::Col}" xold = x .* x; ^ */ 如能立即作出答复,我将不胜感激。

这在犰狳文档中有解释。

请参阅有关运算符的部分,其中指出%用于逐元素乘法:

mat c =  v % b;