The value replied by mxGetPr is a pointer to the data of the mxArray and therefore a (double *). (mxArray *) is a pointer to a struct. In consequence this does not work:
mxArray *Ar; % **wrong** Ar = mxGetPr(plhs[0]); % **wrong**
Correct:
double *Ar; Ar = mxGetPr(plhs[0]);
Or equivalent:
Ar = (double *) mxGetData(plhs[0]);
Modern compilers use the fast SSE unit to copy memory blocks in chunks of 128 or 256 bits, if you call MEMCPY. Therefore I suggest this:
plhs[0] = mxCreateNumericArray(I,J,mxDOUBLE_CLASS,mxREAL); memcpy(mxGetPr(plhs[0]), mxGetPr(prhs[0]), M * sizeof(double));