title: Complex mxArray cumulative elements sum
body: <p>I'm trying to have the sum of a complex mxArray elements (which has to be done for the real and imaginary part separately I believe) .. here's my code </p>
  
  <pre><code> #include &lt;boost/math/special_functions/bessel.hpp&gt;
   #include &lt;boost/math/special_functions/legendre.hpp&gt;
   #include &lt;iostream&gt;
   #include &lt;cmath&gt;
   #include &lt;complex&gt;
   #include "mex.h"
   #include "math.h"
   #include "matrix.h"
  
   using namespace std;
  
  
  void mexFunction (int nlhs,mxArray *plhs[],int nrhs, mxArray *prhs[])
  {
      mxArray *wave_num,*rho,*phi, *r,*data3,*img_mat,*out;
      int k;
      double *vr,*vi,*j_sphere_bessel, *n_sphere_bessel, *mul_j_p,*mat_1,*mat_2,*R_ptr,*I_ptr;
      double rho1,phi1,r1,R,I;
      double *rptr, *iptr;
  
  
  
      wave_num = prhs[0];
      k = (int)(mxGetScalar(wave_num)); 
  
      vr = mxGetPr(prhs[1]);
      vi = mxGetPi(prhs[1]);
  
      r = prhs[2];
      r1 = (double)(mxGetScalar(r));
  
      rho = prhs[3];
      rho1 = (double)(mxGetScalar(rho));
  
      phi = prhs[4];
      phi1 = (double)(mxGetScalar(phi));
  
  
      j_sphere_bessel     = (double *) mxMalloc(1*5 * sizeof(double));
      n_sphere_bessel     = (double *) mxMalloc(1*5 * sizeof(double));
  
      mul_j_p =  (double *) mxMalloc(1*5 * sizeof(double)); 
      mat_1   =  (double *) mxMalloc(1*5 * sizeof(double));
      mat_2   =  (double *) mxMalloc(1*5 * sizeof(double));
  
  
      rptr = mxGetPr(img_mat); // ADDED
      iptr = mxGetPi(img_mat); // ADDED
  
      img_mat = mxCreateDoubleMatrix(1, 1, mxCOMPLEX);
  
      plhs[0] = mxCreateDoubleMatrix(1, 5, mxREAL);   
  
      for (int o=0;o&lt;5;o++)
      {       
          j_sphere_bessel[o]  =     boost::math::sph_bessel(o, k*rho1);
  
          n_sphere_bessel[o]  =    -boost::math::sph_neumann(o, k*rho1);
  
          mul_j_p [o]  = ((2*o)+1)*(boost::math::sph_bessel(o, k*r1))*(boost::math::legendre_p(o, cos(phi1*3.14159265/180))); 
  
          mat_1 [o]  = mul_j_p[o]*j_sphere_bessel[o];
          mat_2 [o]  = mul_j_p[o]*n_sphere_bessel[o];
          R = R + mat_1[o];
          I = I + mat_2[o]; 
  
        rptr[o] = R; // ADDED
        iptr[o] = I; // ADDED // Here I want R to be the real part of the already created matrix img_mat (also I as imaginary part)
  
      }
  
      plhs[0]=img_mat;
  
      mexPrintf("  %  ",plhs[0]);
  
  }
  </code></pre>
  
  <p>Now after summing all elements of mat_1 and mat_2 into (R and I) respectively, I'm trying to set (R) as real part of the already created matrix img_mat and also (I) as imaginary part but nothing seems to be working.<br>
  Can somebody please help me?
  Thanks in advance.</p>