Advertisement
Guest User

matrix element-wise multiplication

a guest
Feb 23rd, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #define ARMA_USE_MKL_ALLOC
  2. #include <armadillo>
  3. #include "mex.h"
  4. using namespace arma;
  5.  
  6. void armaSetPr(mxArray *matlabMatrix, const Mat<double>& armaMatrix)
  7. {
  8.     double *dst_pointer = mxGetPr(matlabMatrix);
  9.     const double *src_pointer = armaMatrix.memptr();
  10.     std::memcpy(dst_pointer, src_pointer, sizeof(double)*armaMatrix.n_elem);
  11. }
  12.  
  13. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  14. {
  15.   uword tnum = (uword) mxGetScalar(prhs[0]);
  16.   uword a = (uword) mxGetScalar(prhs[1]);
  17.   uword b = (uword) mxGetScalar(prhs[2]);
  18.   uword c = (uword) mxGetScalar(prhs[3]);
  19.   uword d = (uword) mxGetScalar(prhs[4]);
  20.  
  21.   mat Ez = Mat<double>(mxGetPr(prhs[5]), (uword) mxGetM(prhs[5]),
  22.     (uword) mxGetN(prhs[5]), true, true);
  23.   mat Hx = Mat<double>(mxGetPr(prhs[6]), (uword) mxGetM(prhs[6]),
  24.     (uword) mxGetN(prhs[6]), true, true);
  25.   mat A = Mat<double>(mxGetPr(prhs[7]), (uword) mxGetM(prhs[7]),
  26.     (uword) mxGetN(prhs[7]), true, true);
  27.   mat B = Mat<double>(mxGetPr(prhs[8]), (uword) mxGetM(prhs[8]),
  28.     (uword) mxGetN(prhs[8]), true, true);
  29.   mat C = Mat<double>(mxGetPr(prhs[9]), (uword) mxGetM(prhs[9]),
  30.     (uword) mxGetN(prhs[9]), true, true);
  31.   mat D = Mat<double>(mxGetPr(prhs[10]), (uword) mxGetM(prhs[10]),
  32.     (uword) mxGetN(prhs[10]), true, true);
  33.  
  34.   for (uword t = 0; t < tnum; t++)
  35.   {
  36.     Ez.submat(a-1, c-1, b-1, d-1) = A % Ez.submat(a-1, c-1, b-1, d-1) +
  37.             B % (Hx.submat(a, c-1, b, d-1) - Hx.submat(a-1, c-1, b-1, d-1));
  38.     Hx.submat(a-1, c-1, b-1, d-1) = C % Hx.submat(a-1, c-1, b-1, d-1) +
  39.             D % (Ez.submat(a, c-1, b, d-1) - Ez.submat(a-1, c-1, b-1, d-1));
  40.   }
  41.  
  42.   plhs[0] = mxCreateDoubleMatrix(Ez.n_rows, Ez.n_cols, mxREAL);
  43.   armaSetPr(plhs[0], Ez);
  44.   plhs[1] = mxCreateDoubleMatrix(Hx.n_rows, Hx.n_cols, mxREAL);
  45.   armaSetPr(plhs[1], Ez);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement