/* Main gateway */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int ndims, len, i; int *dims = NULL; double a, b; double *o = NULL; if (nlhs > 1) mexErrMsgTxt("Too many output arguments."); if (nrhs < 2) mexErrMsgTxt("Requires at least two input arguments."); ndims = (nrhs == 2) ? 1 : nrhs-2; dims = (int*)mxMalloc(ndims*sizeof(int)); len = 1; for (i=0;i<ndims;i++) { dims[i] = (nrhs == 2) ? 1 : (int)mxGetScalar(prhs[i+2]); len *= dims[i]; } a = mxGetScalar(prhs[0]); b = mxGetScalar(prhs[1]); plhs[0] = mxCreateNumericArray(ndims, dims, mxDOUBLE_CLASS, mxREAL); o = mxGetPr(plhs[0]); for(i=0;i<len;i++) *o++ = b * GammaRand(a); mxFree(dims); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize ndims, len, i; mwSize *dims; double *indata, *outdata; if((nlhs > 1) || (nrhs != 1)) mexErrMsgTxt("Usage: x = randgamma(a)"); /* prhs[0] is first argument. * mxGetPr returns double* (data, col-major) */ ndims = mxGetNumberOfDimensions(prhs[0]); dims = (mwSize*)mxGetDimensions(prhs[0]); indata = mxGetPr(prhs[0]); len = mxGetNumberOfElements(prhs[0]); if(mxIsSparse(prhs[0])) mexErrMsgTxt("Cannot handle sparse matrices. Sorry."); /* plhs[0] is first output */ plhs[0] = mxCreateNumericArrayE(ndims, dims, mxDOUBLE_CLASS, mxREAL); outdata = mxGetPr(plhs[0]); for(i=0;i<len;i++) { *outdata++ = GammaRand(*indata++); } }