Sorry, that version did not mimic the shape of the input as sub2ind does: here is a corrected version.
#include <matrix.h>
#include <mex.h>
#include <math.h>
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
//Declare variables
int i,j;
//Load dimensions
double *dims = mxGetPr(prhs[0]);
int ndims = mxGetNumberOfElements(prhs[0]);
int ninputs = mxGetNumberOfElements(prhs[1]);
//Precompute submatrices dimension (cumulative product of each dimension size)
mxArray *cp_mx = mxCreateDoubleMatrix(ndims, 1, mxREAL);
double *cp = mxGetPr(cp_mx);
cp[0] = 1;
for (i = 1; i < ndims; i++)
cp[i] = cp[i1] * dims[i1];
//Associate outputs
plhs[0] = mxDuplicateArray(prhs[1]);
double *indices = mxGetPr(plhs[0]);
//Initialize indices to 1 (in matlab, indices start from 1)
for (j = 0; j < ninputs; j++)
indices[j] = 1;
//Perform the actual computation
double *cur_dim_subs;
for (i = 0; i < ndims; i++) {
//Load current dimension subscripts
cur_dim_subs = mxGetPr(prhs[1+i]);
for (j = 0; j < ninputs; j++) {
indices[j] = indices[j] + (cur_dim_subs[j]1) * cp[i];
}
}
return;
}
"Marco" wrote in message <kr0m0a$4iu$1@newscl01ah.mathworks.com>...
> Hello, I implemented a C version. It is just a basic version, meaning that it doesn't perform any check on the input data; everyone is welcome to improve it.
>
> I don't know how to post a file, so I will paste the source code.
>
>
>
>
>
> #include <matrix.h>
> #include <mex.h>
> #include <math.h>
>
> /**
> * Get the dimension of a onedimension vector (es: 1x3 returns 3, 3x1 returns 3 as well)
> */
> inline int getDim (const mxArray *a) {
> const mwSize *sizes = mxGetDimensions(a);
> return (int)(sizes[0] >= sizes[1] ? sizes[0] : sizes[1]);
> }
>
> void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
>
> //Declare variables
> int i,j;
>
> //Load dimensions
> double *dims = mxGetPr(prhs[0]);
> int ndims = getDim(prhs[0]);
> int ninputs = getDim(prhs[1]);
>
> //Precompute submatrices dimension (cumulative product of each dimension size)
> mxArray *cp_mx = mxCreateDoubleMatrix(ndims, 1, mxREAL);
> double *cp = mxGetPr(cp_mx);
> cp[0] = 1;
> for (i = 1; i < ndims; i++)
> cp[i] = cp[i1] * dims[i1];
>
> //Associate outputs
> plhs[0] = mxCreateDoubleMatrix(ninputs, 1, mxREAL);
> double *indices = mxGetPr(plhs[0]);
>
> //Initialize indices to 1 (in matlab, indices start from 1)
> for (j = 0; j < ninputs; j++)
> indices[j] = 1;
>
> //Perform the actual computation
> double *cur_dim_subs;
> for (i = 0; i < ndims; i++) {
> //Load current dimension subscripts
> cur_dim_subs = mxGetPr(prhs[1+i]);
>
> for (j = 0; j < ninputs; j++) {
> indices[j] = indices[j] + (cur_dim_subs[j]1) * cp[i];
> }
> }
>
> return;
> }
>
>
>
>
> "Matt J" wrote in message <ha11qd$ehi$1@fred.mathworks.com>...
> > Does anyone have a mex implementation of ind2sub & sub2ind ? Couldn't find one on the FEX.
> >
> > I'm a bit surprised there's been no push for more optimized versions than the existing MCoded ones. They seem like pretty fundamental operations...
