Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
ind2sub/sub2ind mex

Subject: ind2sub/sub2ind mex

From: Matt

Date: 1 Oct, 2009 01:49:01

Message: 1 of 4

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 M-Coded ones. They seem like pretty fundamental operations...

Subject: ind2sub/sub2ind mex

From: Marco

Date: 3 Jul, 2013 08:05:30

Message: 2 of 4

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 one-dimension 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[i-1] * dims[i-1];

//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 M-Coded ones. They seem like pretty fundamental operations...

Subject: ind2sub/sub2ind mex

From: Marco

Date: 3 Jul, 2013 08:21:11

Message: 3 of 4

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[i-1] * dims[i-1];

//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 one-dimension 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[i-1] * dims[i-1];
>
> //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 M-Coded ones. They seem like pretty fundamental operations...

Subject: ind2sub/sub2ind mex

From: dpb

Date: 3 Jul, 2013 14:05:33

Message: 4 of 4

On 7/3/2013 3:21 AM, Marco wrote:
...[code listing elided for brevity]...


> "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.
>>
...

That's what file exchange is for...if there isn't something there, this
would be a likely candidate once it's fully debugged/tested...

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us