/* C implementation of repmat. * by Tom Minka */ /* mex -c mexutil.c mex repmat.c mexutil.obj to check for warnings: gcc -Wall -I/cygdrive/c/MATLAB6p1/extern/include -c repmat.c */ #include "mexutil.h" #include /* ALWAYS_2D == 1 gives the usual matlab behavior where repmat(0,3) is * a 3x3 matrix. * ALWAYS_2D == 0 gives a 3x1 matrix instead. * repmat(x,sizes,1) is another way to get this behavior. */ #define ALWAYS_2D 1 /* repeat a block of memory rep times */ void memrep(char *dest, size_t chunk, int rep) { #if 0 /* slow way */ int i; char *p = dest; for(i=1;i>1); #endif } void repmat(char *dest, const char *src, int ndim, int *destdimsize, int *dimsize, const int *dims, int *rep) { int d = ndim-1; int i, chunk; /* copy the first repetition into dest */ if(d == 0) { chunk = dimsize[0]; memcpy(dest,src,chunk); } else { /* recursively repeat each slice of src */ for(i=0;i ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i 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); } }