/* The gateway routine */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *x, *y; int m, n, i; /* Check for proper number of arguments. */ if(nrhs!=1) mexErrMsgTxt("One input required."); if(nlhs!=1) mexErrMsgTxt("One output required."); /* Create a pointer to the input */ x = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input */ m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); /* Set the output pointer to the output matrix. plhs[0] = mxCreateDoubleMatrix(my,ny, mxREAL); Create uninitialized matrix for speed up */ plhs[0] = mxCreateDoubleMatrixE(m,n,mxREAL); /* Create a C pointer to a copy of the output matrix. */ y = mxGetPr(plhs[0]); for (i=0; i < n*m; i++) { *y = 1. - erfnc( *x ); x++; y++; } }
/* The gateway routine */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *x, *y,*z; int mx,my,nx,ny; /* Check for proper number of arguments. */ if(nrhs!=2) mexErrMsgTxt("Two inputs required."); if(nlhs!=1) mexErrMsgTxt("One output required."); /* Create a pointer to the input y. */ y = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input y. */ my = mxGetM(prhs[0]); ny = mxGetN(prhs[0]); /* Create a pointer to the input x. */ x = mxGetPr(prhs[1]); /* Get the dimensions of the matrix input x. */ mx = mxGetM(prhs[1]); nx = mxGetN(prhs[1]); /* Set the output pointer to the output matrix. plhs[0] = mxCreateDoubleMatrix(my,ny, mxREAL); Create uninitialized matrix for speed up */ plhs[0] = mxCreateDoubleMatrixE(my,ny,mxREAL); /* Create a C pointer to a copy of the output matrix. */ z = mxGetPr(plhs[0]); if (mx==1 && nx==ny){ xtimesyrow(x,y,z,my,ny); } else if (nx==1 && mx==my){ xtimesycol(x,y,z,my,ny); } else if (nx==1 && mx==1){ xtimesy(*x,y,z,my,ny); } else { mexErrMsgTxt("Do not match dim"); } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int m,n,i,sz; double *p; if(nrhs != 2) mexErrMsgTxt("xones(m,n)"); if(mxGetNumberOfElements(prhs[0]) != 1) mexErrMsgTxt("m is not scalar"); if(mxGetNumberOfElements(prhs[1]) != 1) mexErrMsgTxt("n is not scalar"); m = (int)*mxGetPr(prhs[0]); n = (int)*mxGetPr(prhs[1]); sz = m*n; #if 0 plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); p = (double*)mxGetPr(plhs[0]); #else plhs[0] = mxCreateDoubleMatrixE(m,n,mxREAL); p = (double*)mxGetPr(plhs[0]); #endif for(i=0;i<sz;i++) *p++ = 1; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mxArray *srcmat; int ndim, eltsize; mwSize *dimsize; const mwSize *dims; int ndimdest; mwSize *destdims, *destdimsize; char *src, *dest; mwSize *rep; int i,nrep; int extra_rep = 1; int empty; double *outp, *inp; mwSize m,n,numel; if(nrhs < 2) mexErrMsgTxt("Usage: repmat(A, [M N ...])"); srcmat = prhs[0]; if(0) { /* testing code, please ignore */ /* plhs[0] = mxCreateNumericArrayE(ndim, dims, mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); */ m = mxGetM(srcmat); n = mxGetN(srcmat); plhs[0] = mxCreateDoubleMatrixE(m, n, mxREAL); outp = mxGetPr(plhs[0]); inp = mxGetPr(srcmat); numel = mxGetNumberOfElements(srcmat); memcpy(outp, inp, numel*sizeof(double)); /* plhs[0] = mxCreateNumericMatrixE(dims[0], dims[1], mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); plhs[0] = mxCreateNumericMatrix(0, 0, mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); */ return; } if(!mxIsNumeric(srcmat) || mxIsSparse(srcmat) || mxIsCell(srcmat) || mxIsStruct(srcmat)) { /* call Matlab's repmat */ mexCallMATLAB(nlhs,plhs,nrhs,(mxArray**)prhs,"xrepmat");return; /* mexErrMsgTxt("Sorry, can't handle sparse matrices yet."); */ } ndim = mxGetNumberOfDimensions(srcmat); dims = mxGetDimensions(srcmat); eltsize = mxGetElementSize(srcmat); /* compute dimension sizes */ dimsize = (mwSize*)mxCalloc(ndim, sizeof(mwSize)); dimsize[0] = eltsize*dims[0]; for(i=1;i<ndim;i++) dimsize[i] = dimsize[i-1]*dims[i]; /* determine repetition vector */ ndimdest = ndim; if(nrhs == 2) { /* prhs[1] is a vector of reps */ nrep = mxGetN(prhs[1]); if(nrep > ndimdest) ndimdest = nrep; rep = (mwSize*)mxCalloc(ndimdest, sizeof(mwSize)); for(i=0;i<nrep;i++) { double repv = mxGetPr(prhs[1])[i]; rep[i] = (mwSize)repv; } #if ALWAYS_2D if(nrep == 1) { /* special behavior */ nrep = 2; rep[1] = rep[0]; } #endif } else { /* concatenate all prhs's */ int ri=0; nrep = 0; for(i=0;i<nrhs-1;i++) { nrep += mxGetNumberOfElements(prhs[i+1]); } if(nrep > ndimdest) ndimdest = nrep; rep = (mwSize*)mxCalloc(ndimdest, sizeof(mwSize)); for(i=0;i<nrhs-1;i++) { double *p = mxGetPr(prhs[i+1]); int j, sz = mxGetNumberOfElements(prhs[i+1]); for(j=0;j<sz;j++) rep[ri++] = (mwSize)p[j]; } } for(i=nrep;i<ndimdest;i++) rep[i] = 1; /* compute output size */ destdims = (mwSize*)mxCalloc(ndimdest, sizeof(mwSize)); for(i=0;i<ndim;i++) destdims[i] = dims[i]*rep[i]; for(;i<ndimdest;i++) { destdims[i] = rep[i]; extra_rep *= rep[i]; } destdimsize = (mwSize*)mxCalloc(ndim, sizeof(mwSize)); destdimsize[0] = eltsize*destdims[0]; for(i=1;i<ndim;i++) destdimsize[i] = destdimsize[i-1]*destdims[i]; /* for speed, array should be uninitialized */ plhs[0] = mxCreateNumericArrayE(ndimdest, destdims, mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); /* if any rep[i] == 0, output should be empty array. Added by KPM 11/13/02. */ empty = 0; for (i=0; i < nrep; i++) { if (rep[i]==0) empty = 1; } if (empty) return; src = (char*)mxGetData(srcmat); dest = (char*)mxGetData(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); if(mxIsComplex(srcmat)) { src = (char*)mxGetPi(srcmat); dest = (char*)mxGetPi(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); } }