here's the code:
/*Unravel.c*/
#include "mex.h"
void unravel(uint16_T *hx, double *link, double *x, double xsz, int hxsz)
{
int i = 15, j = 0, k = 0; int n = 0; while(xsz - k) { if (*(link + n)>0) { if ((*(hx + j)>>i) & 0x0001) { n = *(link + n); } else n = *(link + n) - 1; if (i) i--; else {j++;i = 15;} if (j > hxsz) mexErrMsgTxt("out of code bits ???"); } else { *(x + k++) = -*(link + n); n = 0; } } if (k == xsz - 1)\ *(x + k++) = -*(link + n); }void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ double *link, *x, xsz; uint16_T *hx; int hxsz; /*check for reasonableness*/ if(nrhs != 3) mexErrMsgTxt("3 inputs required."); else if (nlhs > 1) mexErrMsgTxt("Too many output arguments"); /*is the last input a scalar*/ if (!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2] * mxGetM(prhs[2]) != 1) { mexErrMsgTxt("input XSIZE must be a scalar."); } hx = (uint16_T *) mxGetData(prhs[0]); link = (double *) mxGetData(prhs[1]); xsz = mxGetScalar(prhs[2]); /* returns DOUBLE */ /* Get the number of elemnts in hx */ hxsz = mxGetM(prhs[0]); /* Create 'xsz' x 1 output matrix */ plhs[0] = mxCreateDoubleMatrix(xsz, 1, mxREAL); /* Get C pointer to a copy of the output matrix */ x = (double *) mxGetData(plhs[0]); /* Call the C subroutine */ unravel(hx, link, x, xsz, hxsz);} }
Best Answer