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:
Mex function with more than 1 input and output multidimensional arrays

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 22 Aug, 2013 05:02:06

Message: 1 of 20

I'm trying to write a mex function in C which is to be used in a program for further calculations. The function should take a nXm size float array, 1 integer variable as inputs and there are 4 outputs of nXm size each. I'm confused how to use Mex Function in this scenario. Can I do something like this in the main program: -

[a,b,c,d] = mexFunction(q,w);

if I follow this regular MATLAB function format, how would the mexFunction be in definition.. are
int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]
assigned automatically? also can i create separate output variables in mexFunction and assign the pointers to plhs[]. please guide me on this. Thanks

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 22 Aug, 2013 07:43:05

Message: 2 of 20

"Hariprasad" wrote in message <kv460d$44b$1@newscl01ah.mathworks.com>...
> I'm trying to write a mex function in C which is to be used in a program for further calculations. The function should take a nXm size float array, 1 integer variable as inputs and there are 4 outputs of nXm size each. I'm confused how to use Mex Function in this scenario. Can I do something like this in the main program: -
>
> [a,b,c,d] = mexFunction(q,w);
>
> if I follow this regular MATLAB function format, how would the mexFunction be in definition.. are
> int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]
> assigned automatically? also can i create separate output variables in mexFunction and assign the pointers to plhs[]. please guide me on this. Thanks

Here is an example mex file that takes two inputs, an nxm float array and an integer. Produces 4 double outputs.

// [a b c d] = mymex(f,k)
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    mwSize m, n;
    int i;
    float *fp;
    double *dp1, *dp2, *dp3, *dp4;

    if( nrhs != 2 ) {
        mexErrMsgTxt("Need two inputs");
    }
    if( nlhs != 4 ) {
        mexErrMsgTxt("Need four outputs");
    }
    if( !mxIsSingle(prhs[0]) ) {
        mexErrMsgTxt("First input must be single");
    }
    // plus add other input checks here
    m = mxGetM(prhs[0]);
    n = mxGetN(prhs[0]);
    fp = mxGetData(prhs[0]);
    i = mxGetScalar(prhs[1]);
    plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
    plhs[1] = mxCreateDoubleMatrix(m,n,mxREAL);
    plhs[2] = mxCreateDoubleMatrix(m,n,mxREAL);
    plhs[3] = mxCreateDoubleMatrix(m,n,mxREAL);
    dp1 = mxGetPr(plhs[0]);
    dp2 = mxGetPr(plhs[1]);
    dp3 = mxGetPr(plhs[2]);
    dp4 = mxGetPr(plhs[3]);
    // add code here to manipulate data behind fp, dp1, etc.
}

James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 22 Aug, 2013 09:16:16

Message: 3 of 20

"James Tursa" wrote in message <kv4fe9$seb$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv460d$44b$1@newscl01ah.mathworks.com>...
> > I'm trying to write a mex function in C which is to be used in a program for further calculations. The function should take a nXm size float array, 1 integer variable as inputs and there are 4 outputs of nXm size each. I'm confused how to use Mex Function in this scenario. Can I do something like this in the main program: -
> >
> > [a,b,c,d] = mexFunction(q,w);
> >
> > if I follow this regular MATLAB function format, how would the mexFunction be in definition.. are
> > int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]
> > assigned automatically? also can i create separate output variables in mexFunction and assign the pointers to plhs[]. please guide me on this. Thanks
>
> Here is an example mex file that takes two inputs, an nxm float array and an integer. Produces 4 double outputs.
>
> // [a b c d] = mymex(f,k)
> #include "mex.h"
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
> {
> mwSize m, n;
> int i;
> float *fp;
> double *dp1, *dp2, *dp3, *dp4;
>
> if( nrhs != 2 ) {
> mexErrMsgTxt("Need two inputs");
> }
> if( nlhs != 4 ) {
> mexErrMsgTxt("Need four outputs");
> }
> if( !mxIsSingle(prhs[0]) ) {
> mexErrMsgTxt("First input must be single");
> }
> // plus add other input checks here
> m = mxGetM(prhs[0]);
> n = mxGetN(prhs[0]);
> fp = mxGetData(prhs[0]);
> i = mxGetScalar(prhs[1]);
> plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
> plhs[1] = mxCreateDoubleMatrix(m,n,mxREAL);
> plhs[2] = mxCreateDoubleMatrix(m,n,mxREAL);
> plhs[3] = mxCreateDoubleMatrix(m,n,mxREAL);
> dp1 = mxGetPr(plhs[0]);
> dp2 = mxGetPr(plhs[1]);
> dp3 = mxGetPr(plhs[2]);
> dp4 = mxGetPr(plhs[3]);
> // add code here to manipulate data behind fp, dp1, etc.
> }
>
> James Tursa


Thank you Sir.. i have another doubt.. i also have some C functions for example

float ** rep_mat(float data,int rows,int cols) // like a repmat function in MATLAB
{
    int i, j;
    float **output = malloc(sizeof(float*) * rows);
    for(i=0; i< rows; i++)
        output = malloc(sizeof(float) * cols);
    
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            output[i][j] = data;
        }
    }
    return(output);
}

so can I do smthing like this

float Outdata[2][4] = rep_mat(data,2,4);

inside the mexFunction.
I think this would solve a lot of problems in my code. Thanks

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 22 Aug, 2013 15:57:21

Message: 4 of 20

"Hariprasad" wrote in message <kv4kt0$j6m$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <kv4fe9$seb$1@newscl01ah.mathworks.com>...
> > "Hariprasad" wrote in message <kv460d$44b$1@newscl01ah.mathworks.com>...
> > > I'm trying to write a mex function in C which is to be used in a program for further calculations. The function should take a nXm size float array, 1 integer variable as inputs and there are 4 outputs of nXm size each. I'm confused how to use Mex Function in this scenario. Can I do something like this in the main program: -
> > >
> > > [a,b,c,d] = mexFunction(q,w);
> > >
> > > if I follow this regular MATLAB function format, how would the mexFunction be in definition.. are
> > > int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]
> > > assigned automatically? also can i create separate output variables in mexFunction and assign the pointers to plhs[]. please guide me on this. Thanks
> >
> > Here is an example mex file that takes two inputs, an nxm float array and an integer. Produces 4 double outputs.
> >
> > // [a b c d] = mymex(f,k)
> > #include "mex.h"
> > void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
> > {
> > mwSize m, n;
> > int i;
> > float *fp;
> > double *dp1, *dp2, *dp3, *dp4;
> >
> > if( nrhs != 2 ) {
> > mexErrMsgTxt("Need two inputs");
> > }
> > if( nlhs != 4 ) {
> > mexErrMsgTxt("Need four outputs");
> > }
> > if( !mxIsSingle(prhs[0]) ) {
> > mexErrMsgTxt("First input must be single");
> > }
> > // plus add other input checks here
> > m = mxGetM(prhs[0]);
> > n = mxGetN(prhs[0]);
> > fp = mxGetData(prhs[0]);
> > i = mxGetScalar(prhs[1]);
> > plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
> > plhs[1] = mxCreateDoubleMatrix(m,n,mxREAL);
> > plhs[2] = mxCreateDoubleMatrix(m,n,mxREAL);
> > plhs[3] = mxCreateDoubleMatrix(m,n,mxREAL);
> > dp1 = mxGetPr(plhs[0]);
> > dp2 = mxGetPr(plhs[1]);
> > dp3 = mxGetPr(plhs[2]);
> > dp4 = mxGetPr(plhs[3]);
> > // add code here to manipulate data behind fp, dp1, etc.
> > }
> >
> > James Tursa
>
>
> Thank you Sir.. i have another doubt.. i also have some C functions for example
>
> float ** rep_mat(float data,int rows,int cols) // like a repmat function in MATLAB
> {
> int i, j;
> float **output = malloc(sizeof(float*) * rows);
> for(i=0; i< rows; i++)
> output = malloc(sizeof(float) * cols);
>
> for(i = 0; i < rows; i++)
> {
> for(j = 0; j < cols; j++)
> {
> output[i][j] = data;
> }
> }
> return(output);
> }
>
> so can I do smthing like this
>
> float Outdata[2][4] = rep_mat(data,2,4);
>
> inside the mexFunction.
> I think this would solve a lot of problems in my code. Thanks

No, you can't do that. C does not do whole array assignments like Fortran and MATLAB. You need to copy the data one element at a time in a loop (or perhaps use memcpy on the rows). E.g.,

float data;
float Outdata[2][4];
float **output;
int i, j, rows, cols;
    :
output = rep_mat(data, rows, cols);
for(i = 0; i < rows; i++)
{
    for(j = 0; j < cols; j++)
    {
        Outdata[i][j] = output[i][j];
    }
}

Having said that, there are better ways to do this that can avoid data copies. And your method of allocating output is not guaranteed to end up with contiguous data for the 2D matrix. It would be better to allocate the memory for the entire 2D matrix with one malloc (or better mxMalloc) call, and then work with the single pointer to the starting address of this block in your code. Using the [i][j] syntax for getting the elements looks nice at the outset, but it will cause headaches downstream in your code, particularly if you are going back & forth between that notation and a mxArray that gets passed into or out of your mex function. E.g., this is what I would do if the matrix was to be passed back to the caller in an mxArray:

float data;
float *fp;
int i, rows, cols, rc;
    :
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
fp = mxGetData(plhs[0]);
rc = rows * cols;
for( i=0; i<rc; i++ ) {
    fp[i] = data;
}


James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 05:34:09

Message: 5 of 20

"James Tursa" wrote in message <kv5cd1$3r0$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv4kt0$j6m$1@newscl01ah.mathworks.com>...
> > "James Tursa" wrote in message <kv4fe9$seb$1@newscl01ah.mathworks.com>...
> > > "Hariprasad" wrote in message <kv460d$44b$1@newscl01ah.mathworks.com>...
> > > > I'm trying to write a mex function in C which is to be used in a program for further calculations. The function should take a nXm size float array, 1 integer variable as inputs and there are 4 outputs of nXm size each. I'm confused how to use Mex Function in this scenario. Can I do something like this in the main program: -
> > > >
> > > > [a,b,c,d] = mexFunction(q,w);
> > > >
> > > > if I follow this regular MATLAB function format, how would the mexFunction be in definition.. are
> > > > int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]
> > > > assigned automatically? also can i create separate output variables in mexFunction and assign the pointers to plhs[]. please guide me on this. Thanks
> > >
> > > Here is an example mex file that takes two inputs, an nxm float array and an integer. Produces 4 double outputs.
> > >
> > > // [a b c d] = mymex(f,k)
> > > #include "mex.h"
> > > void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
> > > {
> > > mwSize m, n;
> > > int i;
> > > float *fp;
> > > double *dp1, *dp2, *dp3, *dp4;
> > >
> > > if( nrhs != 2 ) {
> > > mexErrMsgTxt("Need two inputs");
> > > }
> > > if( nlhs != 4 ) {
> > > mexErrMsgTxt("Need four outputs");
> > > }
> > > if( !mxIsSingle(prhs[0]) ) {
> > > mexErrMsgTxt("First input must be single");
> > > }
> > > // plus add other input checks here
> > > m = mxGetM(prhs[0]);
> > > n = mxGetN(prhs[0]);
> > > fp = mxGetData(prhs[0]);
> > > i = mxGetScalar(prhs[1]);
> > > plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
> > > plhs[1] = mxCreateDoubleMatrix(m,n,mxREAL);
> > > plhs[2] = mxCreateDoubleMatrix(m,n,mxREAL);
> > > plhs[3] = mxCreateDoubleMatrix(m,n,mxREAL);
> > > dp1 = mxGetPr(plhs[0]);
> > > dp2 = mxGetPr(plhs[1]);
> > > dp3 = mxGetPr(plhs[2]);
> > > dp4 = mxGetPr(plhs[3]);
> > > // add code here to manipulate data behind fp, dp1, etc.
> > > }
> > >
> > > James Tursa
> >
> >
> > Thank you Sir.. i have another doubt.. i also have some C functions for example
> >
> > float ** rep_mat(float data,int rows,int cols) // like a repmat function in MATLAB
> > {
> > int i, j;
> > float **output = malloc(sizeof(float*) * rows);
> > for(i=0; i< rows; i++)
> > output = malloc(sizeof(float) * cols);
> >
> > for(i = 0; i < rows; i++)
> > {
> > for(j = 0; j < cols; j++)
> > {
> > output[i][j] = data;
> > }
> > }
> > return(output);
> > }
> >
> > so can I do smthing like this
> >
> > float Outdata[2][4] = rep_mat(data,2,4);
> >
> > inside the mexFunction.
> > I think this would solve a lot of problems in my code. Thanks
>
> No, you can't do that. C does not do whole array assignments like Fortran and MATLAB. You need to copy the data one element at a time in a loop (or perhaps use memcpy on the rows). E.g.,
>
> float data;
> float Outdata[2][4];
> float **output;
> int i, j, rows, cols;
> :
> output = rep_mat(data, rows, cols);
> for(i = 0; i < rows; i++)
> {
> for(j = 0; j < cols; j++)
> {
> Outdata[i][j] = output[i][j];
> }
> }
>
> Having said that, there are better ways to do this that can avoid data copies. And your method of allocating output is not guaranteed to end up with contiguous data for the 2D matrix. It would be better to allocate the memory for the entire 2D matrix with one malloc (or better mxMalloc) call, and then work with the single pointer to the starting address of this block in your code. Using the [i][j] syntax for getting the elements looks nice at the outset, but it will cause headaches downstream in your code, particularly if you are going back & forth between that notation and a mxArray that gets passed into or out of your mex function. E.g., this is what I would do if the matrix was to be passed back to the caller in an mxArray:
>
> float data;
> float *fp;
> int i, rows, cols, rc;
> :
> plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
> fp = mxGetData(plhs[0]);
> rc = rows * cols;
> for( i=0; i<rc; i++ ) {
> fp[i] = data;
> }
>
>
> James Tursa

Actually 'rep_mat' is one of the functions in C which is used only inside mexFunction. The output from rep_mat and other functions(giving mxn array as output) are used for further calculations only inside the mexFunction to compute the 4 mxn float arrays as output of mexFunction. And this mexFunction is to be used as normal MATLAB Function in the main MATLAB program.
Sorry, this being my first attempt with MEX and C in MATLAB, i feel confused on how to go about. Also i have a doubt, what is the difference between mxGetPr and mxGetData. i read up the topics but i could not get it.. could you give me a simple example? will be really helpful. Thanks

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 23 Aug, 2013 06:56:17

Message: 6 of 20

"Hariprasad" wrote in message <kv6s8h$apq$1@newscl01ah.mathworks.com>...
>
> Actually 'rep_mat' is one of the functions in C which is used only inside mexFunction. The output from rep_mat and other functions(giving mxn array as output) ...

An mxn array is *not* returned by rep_mat. What is returned is a pointer. It is set up so that you can use the [i][j] syntax on the pointer (similar to array syntax), but the data is not necessarily contiguous and the variable is not an array.

>... are used for further calculations only inside the mexFunction to compute the 4 mxn float arrays as output of mexFunction. And this mexFunction is to be used as normal MATLAB Function in the main MATLAB program.
> Sorry, this being my first attempt with MEX and C in MATLAB, i feel confused on how to go about. Also i have a doubt, what is the difference between mxGetPr and mxGetData. i read up the topics but i could not get it.. could you give me a simple example? will be really helpful. Thanks

mxGetPr returns a (double *) type and should only be used on mxArray double class variables. It returns a pointer to the first element in the data, or NULL if the variable is empty.

mxGetData returns a (void *) type and can be used on any mxArray variable. For numeric variables, it returns a pointer to the first data element (or NULL if the variable is empty), and should be cast to a pointer to the underlying type before being used. (double *) mxGetData(etc) is equivalent to mxGetPr(etc).

There are companion functions mxGetPi and mxGetImagData for the imaginary part of complex variables. (double *)mxGetData(etc) is equivalent to mxGetPi(etc).

James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 07:17:05

Message: 7 of 20

"James Tursa" wrote in message <kv712h$b0l$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv6s8h$apq$1@newscl01ah.mathworks.com>...
> >
> > Actually 'rep_mat' is one of the functions in C which is used only inside mexFunction. The output from rep_mat and other functions(giving mxn array as output) ...
>
> An mxn array is *not* returned by rep_mat. What is returned is a pointer. It is set up so that you can use the [i][j] syntax on the pointer (similar to array syntax), but the data is not necessarily contiguous and the variable is not an array.
>
> >... are used for further calculations only inside the mexFunction to compute the 4 mxn float arrays as output of mexFunction. And this mexFunction is to be used as normal MATLAB Function in the main MATLAB program.
> > Sorry, this being my first attempt with MEX and C in MATLAB, i feel confused on how to go about. Also i have a doubt, what is the difference between mxGetPr and mxGetData. i read up the topics but i could not get it.. could you give me a simple example? will be really helpful. Thanks
>
> mxGetPr returns a (double *) type and should only be used on mxArray double class variables. It returns a pointer to the first element in the data, or NULL if the variable is empty.
>
> mxGetData returns a (void *) type and can be used on any mxArray variable. For numeric variables, it returns a pointer to the first data element (or NULL if the variable is empty), and should be cast to a pointer to the underlying type before being used. (double *) mxGetData(etc) is equivalent to mxGetPr(etc).
>
> There are companion functions mxGetPi and mxGetImagData for the imaginary part of complex variables. (double *)mxGetData(etc) is equivalent to mxGetPi(etc).
>
> James Tursa

then,
double * array
and
mxArray *array
are different or just different notations for the same thing.

like if i have
double *input_train;
plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
input_train = mxGetPr(plhs[0]);
then can I directly access input_train with indexing [i][j] as a regular double array??

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 23 Aug, 2013 07:53:06

Message: 8 of 20

"Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
>
> then,
> double * array
> and
> mxArray *array
> are different or just different notations for the same thing.

They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.

> like if i have
> double *input_train;
> plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> input_train = mxGetPr(plhs[0]);
> then can I directly access input_train with indexing [i][j] as a regular double array??

No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:

input_train[i + j*m] // the i,j element using 0-based indexing

input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing

If you really want the i,j indexing capability you could set up a macro (which I don't recommend):

#define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing

Then you can get at the i,j element with

INPUT_TRAIN(i,j)

Rather than take this approach, I would advise simply getting used to doing your own linear indexing.

James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 09:49:05

Message: 9 of 20

"James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> >
> > then,
> > double * array
> > and
> > mxArray *array
> > are different or just different notations for the same thing.
>
> They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
>
> > like if i have
> > double *input_train;
> > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > input_train = mxGetPr(plhs[0]);
> > then can I directly access input_train with indexing [i][j] as a regular double array??
>
> No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
>
> input_train[i + j*m] // the i,j element using 0-based indexing
>
> input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
>
> If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
>
> #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
>
> Then you can get at the i,j element with
>
> INPUT_TRAIN(i,j)
>
> Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
>
> James Tursa

Oh!!! so even if its a multidimensional array, i need to consider them as linear ones..
A = [1,2,3 ; 4,5,6] in MATLAB will be {1,2,3,4,5,6} in MEX??

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 09:54:06

Message: 10 of 20

"James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> >
> > then,
> > double * array
> > and
> > mxArray *array
> > are different or just different notations for the same thing.
>
> They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
>
> > like if i have
> > double *input_train;
> > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > input_train = mxGetPr(plhs[0]);
> > then can I directly access input_train with indexing [i][j] as a regular double array??
>
> No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
>
> input_train[i + j*m] // the i,j element using 0-based indexing
>
> input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
>
> If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
>
> #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
>
> Then you can get at the i,j element with
>
> INPUT_TRAIN(i,j)
>
> Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
>
> James Tursa

Oh!!! So an array in MATLAB like A = [1,2,3 ; 4,5,6] which is 2x3
has to be considered as {1,2,3,4,5,6} while using in MEX??
i need to work on this one as well then because i have been writing many C functions with the [i][j] indexing which are used in the mexFunction.

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 12:27:14

Message: 11 of 20

"Hariprasad" wrote in message <kv7bfu$589$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> > "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> > >
> > > then,
> > > double * array
> > > and
> > > mxArray *array
> > > are different or just different notations for the same thing.
> >
> > They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
> >
> > > like if i have
> > > double *input_train;
> > > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > > input_train = mxGetPr(plhs[0]);
> > > then can I directly access input_train with indexing [i][j] as a regular double array??
> >
> > No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
> >
> > input_train[i + j*m] // the i,j element using 0-based indexing
> >
> > input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
> >
> > If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
> >
> > #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
> >
> > Then you can get at the i,j element with
> >
> > INPUT_TRAIN(i,j)
> >
> > Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
> >
> > James Tursa
>
> Oh!!! So an array in MATLAB like A = [1,2,3 ; 4,5,6] which is 2x3
> has to be considered as {1,2,3,4,5,6} while using in MEX??
> i need to work on this one as well then because i have been writing many C functions with the [i][j] indexing which are used in the mexFunction.

i have an issue in this part now: -

float fmin(float *input_train[], int m, int n ,int dim, int rc) // for finding Min of the array
{
float output;
    int i,j;
if(dim == 1) // min(data,[],1) Column chk
{
output = input_train[0][rc]; // pls ignore the indexing
for(i = 0; i < m; i++)
{
if(input_train[i][rc] < output) // pls ignore the indexing
{
output = input_train[i][rc]; // pls ignore the indexing
}
}
}
else if(dim == 2) // min(data,[],2) Row chk
{
output = input_train[rc][0]; // pls ignore the indexing
for(j = 0; j < n; j++)
{
if(input_train[rc][j] < output) // pls ignore the indexing
{
output = input_train[rc][j]; // pls ignore the indexing
}
}
}
return(output);
}
This is a function in C where I find min() in input_train array.
in mexFunction I have
float *input_train;
float min_val;
input_train = (float *)mxGetData(prhs[0]);

when i call the function like

min_val = fmin(input_train,m,n,2,i); // m and n are 2 and arnd750000 respectively.
i get this error: "assignment of pointer to pointer to float to pointer to float"
what mistake have I done in call?
Or is it actually the indexing which is causing the problem? sorry for being so Naive...

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 23 Aug, 2013 14:45:13

Message: 12 of 20

"Hariprasad" wrote in message <kv7bfu$589$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> > "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> > >
> > > then,
> > > double * array
> > > and
> > > mxArray *array
> > > are different or just different notations for the same thing.
> >
> > They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
> >
> > > like if i have
> > > double *input_train;
> > > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > > input_train = mxGetPr(plhs[0]);
> > > then can I directly access input_train with indexing [i][j] as a regular double array??
> >
> > No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
> >
> > input_train[i + j*m] // the i,j element using 0-based indexing
> >
> > input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
> >
> > If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
> >
> > #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
> >
> > Then you can get at the i,j element with
> >
> > INPUT_TRAIN(i,j)
> >
> > Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
> >
> > James Tursa
>
> Oh!!! So an array in MATLAB like A = [1,2,3 ; 4,5,6] which is 2x3
> has to be considered as {1,2,3,4,5,6} while using in MEX??

No. MATLAB orders data in memory in column order, like Fortran. C orders 2D arrays in memory in row order (essentialy the transpose of MATLAB and Fortran). So your A matrix above would be stored linearly in memory as 1, 4, 2, 5, 3, 6.

> i need to work on this one as well then because i have been writing many C functions with the [i][j] indexing which are used in the mexFunction.

Sorry to hear that. This is going to cause you headaches down the road if you need to go back & forth between this and an mxArray.

James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: James Tursa

Date: 23 Aug, 2013 14:58:29

Message: 13 of 20

"Hariprasad" wrote in message <kv7kf2$5ll$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv7bfu$589$1@newscl01ah.mathworks.com>...
> > "James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> > > "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> > > >
> > > > then,
> > > > double * array
> > > > and
> > > > mxArray *array
> > > > are different or just different notations for the same thing.
> > >
> > > They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
> > >
> > > > like if i have
> > > > double *input_train;
> > > > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > > > input_train = mxGetPr(plhs[0]);
> > > > then can I directly access input_train with indexing [i][j] as a regular double array??
> > >
> > > No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
> > >
> > > input_train[i + j*m] // the i,j element using 0-based indexing
> > >
> > > input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
> > >
> > > If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
> > >
> > > #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
> > >
> > > Then you can get at the i,j element with
> > >
> > > INPUT_TRAIN(i,j)
> > >
> > > Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
> > >
> > > James Tursa
> >
> > Oh!!! So an array in MATLAB like A = [1,2,3 ; 4,5,6] which is 2x3
> > has to be considered as {1,2,3,4,5,6} while using in MEX??
> > i need to work on this one as well then because i have been writing many C functions with the [i][j] indexing which are used in the mexFunction.
>
> i have an issue in this part now: -
>
> float fmin(float *input_train[], int m, int n ,int dim, int rc) // for finding Min of the array
> {
> float output;
> int i,j;
> if(dim == 1) // min(data,[],1) Column chk
> {
> output = input_train[0][rc]; // pls ignore the indexing
> for(i = 0; i < m; i++)
> {
> if(input_train[i][rc] < output) // pls ignore the indexing
> {
> output = input_train[i][rc]; // pls ignore the indexing
> }
> }
> }
> else if(dim == 2) // min(data,[],2) Row chk
> {
> output = input_train[rc][0]; // pls ignore the indexing
> for(j = 0; j < n; j++)
> {
> if(input_train[rc][j] < output) // pls ignore the indexing
> {
> output = input_train[rc][j]; // pls ignore the indexing
> }
> }
> }
> return(output);
> }
> This is a function in C where I find min() in input_train array.
> in mexFunction I have
> float *input_train;
> float min_val;
> input_train = (float *)mxGetData(prhs[0]);
>
> when i call the function like
>
> min_val = fmin(input_train,m,n,2,i); // m and n are 2 and arnd750000 respectively.
> i get this error: "assignment of pointer to pointer to float to pointer to float"
> what mistake have I done in call?
> Or is it actually the indexing which is causing the problem? sorry for being so Naive...

This is the headache I was talking about earlier. You are getting the error because of this line:

> float fmin(float *input_train[], int m, int n ,int dim, int rc)

input_train, as declared in the dummy argument list above, is of type (float **). I.e., it is a "pointer to pointer to float". When used in a dummy argument context, the [] do NOT mean "array" ... they mean "pointer to". It would be exactly the same as this:

> float fmin(float **input_train, int m, int n ,int dim, int rc)

So in your calling statement you are using the following for the first argument:

> float *input_train;

So you are passing a (float *) type in a spot that expects a (float **) type, hence the error.

The way to fix this, if you insist on using the [i][j] syntax in your functions (which I do not recomment btw), is to create an array of column pointers in your calling routine and then pass that into your function. But this will be confusing at best because of the column-major order of data in MATLAB vs the row-major order of data in C. So to use your routines as-is you would have to:

1) Manually transpose the entire array (i.e., copy the entire data set into a separate area of memory transposed)

2) Allocate a new array of row pointers

3) Fill in the array of row pointers so they point to the columns of your transposed data (the rows of your original data)

4) Call your functions with these row pointer arrays

5) Deallocate the row pointer arrays

6) Deallocate the transposed data copy

Are you getting a headache yet?

James Tursa

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 23 Aug, 2013 16:26:05

Message: 14 of 20

"James Tursa" wrote in message <kv7tal$c2i$1@newscl01ah.mathworks.com>...
> "Hariprasad" wrote in message <kv7kf2$5ll$1@newscl01ah.mathworks.com>...
> > "Hariprasad" wrote in message <kv7bfu$589$1@newscl01ah.mathworks.com>...
> > > "James Tursa" wrote in message <kv74d2$r4i$1@newscl01ah.mathworks.com>...
> > > > "Hariprasad" wrote in message <kv729h$q75$1@newscl01ah.mathworks.com>...
> > > > >
> > > > > then,
> > > > > double * array
> > > > > and
> > > > > mxArray *array
> > > > > are different or just different notations for the same thing.
> > > >
> > > > They are different. They are both pointers, but they point to different data types. They are *not* different notations for the same thing.
> > > >
> > > > > like if i have
> > > > > double *input_train;
> > > > > plhs[0] = mxCreateDoubleMatrix(m,n,mxReal);
> > > > > input_train = mxGetPr(plhs[0]);
> > > > > then can I directly access input_train with indexing [i][j] as a regular double array??
> > > >
> > > > No, you cannot. input_train is a (double *) and it points to the first element in the data area of plhs[0]. You cannot use [i][j] indexing on input_train. You need to do something like this:
> > > >
> > > > input_train[i + j*m] // the i,j element using 0-based indexing
> > > >
> > > > input_train[(i-1) + (j-1)*m] // the i,j element using 1-based indexing
> > > >
> > > > If you really want the i,j indexing capability you could set up a macro (which I don't recommend):
> > > >
> > > > #define INPUT_TRAIN(i,j) input_train[(i) + (j)*m] // 0-based indexing
> > > >
> > > > Then you can get at the i,j element with
> > > >
> > > > INPUT_TRAIN(i,j)
> > > >
> > > > Rather than take this approach, I would advise simply getting used to doing your own linear indexing.
> > > >
> > > > James Tursa
> > >
> > > Oh!!! So an array in MATLAB like A = [1,2,3 ; 4,5,6] which is 2x3
> > > has to be considered as {1,2,3,4,5,6} while using in MEX??
> > > i need to work on this one as well then because i have been writing many C functions with the [i][j] indexing which are used in the mexFunction.
> >
> > i have an issue in this part now: -
> >
> > float fmin(float *input_train[], int m, int n ,int dim, int rc) // for finding Min of the array
> > {
> > float output;
> > int i,j;
> > if(dim == 1) // min(data,[],1) Column chk
> > {
> > output = input_train[0][rc]; // pls ignore the indexing
> > for(i = 0; i < m; i++)
> > {
> > if(input_train[i][rc] < output) // pls ignore the indexing
> > {
> > output = input_train[i][rc]; // pls ignore the indexing
> > }
> > }
> > }
> > else if(dim == 2) // min(data,[],2) Row chk
> > {
> > output = input_train[rc][0]; // pls ignore the indexing
> > for(j = 0; j < n; j++)
> > {
> > if(input_train[rc][j] < output) // pls ignore the indexing
> > {
> > output = input_train[rc][j]; // pls ignore the indexing
> > }
> > }
> > }
> > return(output);
> > }
> > This is a function in C where I find min() in input_train array.
> > in mexFunction I have
> > float *input_train;
> > float min_val;
> > input_train = (float *)mxGetData(prhs[0]);
> >
> > when i call the function like
> >
> > min_val = fmin(input_train,m,n,2,i); // m and n are 2 and arnd750000 respectively.
> > i get this error: "assignment of pointer to pointer to float to pointer to float"
> > what mistake have I done in call?
> > Or is it actually the indexing which is causing the problem? sorry for being so Naive...
>
> This is the headache I was talking about earlier. You are getting the error because of this line:
>
> > float fmin(float *input_train[], int m, int n ,int dim, int rc)
>
> input_train, as declared in the dummy argument list above, is of type (float **). I.e., it is a "pointer to pointer to float". When used in a dummy argument context, the [] do NOT mean "array" ... they mean "pointer to". It would be exactly the same as this:
>
> > float fmin(float **input_train, int m, int n ,int dim, int rc)
>
> So in your calling statement you are using the following for the first argument:
>
> > float *input_train;
>
> So you are passing a (float *) type in a spot that expects a (float **) type, hence the error.
>
> The way to fix this, if you insist on using the [i][j] syntax in your functions (which I do not recomment btw), is to create an array of column pointers in your calling routine and then pass that into your function. But this will be confusing at best because of the column-major order of data in MATLAB vs the row-major order of data in C. So to use your routines as-is you would have to:
>
> 1) Manually transpose the entire array (i.e., copy the entire data set into a separate area of memory transposed)
>
> 2) Allocate a new array of row pointers
>
> 3) Fill in the array of row pointers so they point to the columns of your transposed data (the rows of your original data)
>
> 4) Call your functions with these row pointer arrays
>
> 5) Deallocate the row pointer arrays
>
> 6) Deallocate the transposed data copy
>
> Are you getting a headache yet?
>
> James Tursa

Oh Lord!!! :-) are there any good text from which I can learn MEX and Pointers?
Im stuck with just the start.. later i have 3-D array to be handled as well !!!

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Steven_Lord

Date: 23 Aug, 2013 19:31:01

Message: 15 of 20



"Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
news:kv82et$nk8$1@newscl01ah.mathworks.com...

*snip*

> Oh Lord!!! :-) are there any good text from which I can learn MEX and
> Pointers?
> Im stuck with just the start.. later i have 3-D array to be handled as
> well !!!

Perhaps these are silly questions, but why are you writing a MEX-file? How
experienced are you with C or C++? Could your needs be met by writing a
MATLAB script, function, or class file instead?

If you're writing a MEX-file because you're heard "MATLAB is slow" try
writing the MATLAB code and profiling it first. We have made many advances
in MATLAB over the years that have greatly improved its performance. Even if
the MATLAB code you write were slower than your MEX-file, at least in my
opinion, MATLAB code is easier to write and easier to debug and that benefit
may outweigh a small slowdown.

If you're writing a MEX-file to interface with some other library or piece
of hardware, check out the LOADLIBRARY function as well as some of the
toolboxes and functions designed to help with that interfacing (the SERIAL
object in MATLAB or the Instrument Control Toolbox, Data Acquisition
Toolbox, etc.)

http://www.mathworks.com/test-measurement/

If you're writing a MEX-file to learn C or C++, perhaps you should focus on
learning C or C++ first, to avoid the added complexity imposed by the
interface between MATLAB and C or C++ that would make it harder for you to
learn.

If you're writing a MEX-file to learn how to work with MEX-files, you may
want to start with a simpler problem.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 24 Aug, 2013 07:41:14

Message: 16 of 20

"Steven_Lord" <slord@mathworks.com> wrote in message <kv8d9l$dgt$1@newscl01ah.mathworks.com>...
>
>
> "Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
> news:kv82et$nk8$1@newscl01ah.mathworks.com...
>
> *snip*
>
> > Oh Lord!!! :-) are there any good text from which I can learn MEX and
> > Pointers?
> > Im stuck with just the start.. later i have 3-D array to be handled as
> > well !!!
>
> Perhaps these are silly questions, but why are you writing a MEX-file? How
> experienced are you with C or C++? Could your needs be met by writing a
> MATLAB script, function, or class file instead?
>
> If you're writing a MEX-file because you're heard "MATLAB is slow" try
> writing the MATLAB code and profiling it first. We have made many advances
> in MATLAB over the years that have greatly improved its performance. Even if
> the MATLAB code you write were slower than your MEX-file, at least in my
> opinion, MATLAB code is easier to write and easier to debug and that benefit
> may outweigh a small slowdown.
>
> If you're writing a MEX-file to interface with some other library or piece
> of hardware, check out the LOADLIBRARY function as well as some of the
> toolboxes and functions designed to help with that interfacing (the SERIAL
> object in MATLAB or the Instrument Control Toolbox, Data Acquisition
> Toolbox, etc.)
>
> http://www.mathworks.com/test-measurement/
>
> If you're writing a MEX-file to learn C or C++, perhaps you should focus on
> learning C or C++ first, to avoid the added complexity imposed by the
> interface between MATLAB and C or C++ that would make it harder for you to
> learn.
>
> If you're writing a MEX-file to learn how to work with MEX-files, you may
> want to start with a simpler problem.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I have a Kmeans clustering function(not the inbuilt) in MATLAB which I need to convert to MEX as the data handled is too huge and MATLAB goes out of Memory with such huge data. Also there are a lot iterations to be done with the huge data. The data size is currently around 1.7million i.e. 2x1.7million double and later it could go up to 4 million. So I'm trying to work around the same algorithm in C (Mex).

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 24 Aug, 2013 08:12:05

Message: 17 of 20

"Hariprasad" wrote in message <kv9o2q$7iu$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <kv8d9l$dgt$1@newscl01ah.mathworks.com>...
> >
> >
> > "Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
> > news:kv82et$nk8$1@newscl01ah.mathworks.com...
> >
> > *snip*
> >
> > > Oh Lord!!! :-) are there any good text from which I can learn MEX and
> > > Pointers?
> > > Im stuck with just the start.. later i have 3-D array to be handled as
> > > well !!!
> >
> > Perhaps these are silly questions, but why are you writing a MEX-file? How
> > experienced are you with C or C++? Could your needs be met by writing a
> > MATLAB script, function, or class file instead?
> >
> > If you're writing a MEX-file because you're heard "MATLAB is slow" try
> > writing the MATLAB code and profiling it first. We have made many advances
> > in MATLAB over the years that have greatly improved its performance. Even if
> > the MATLAB code you write were slower than your MEX-file, at least in my
> > opinion, MATLAB code is easier to write and easier to debug and that benefit
> > may outweigh a small slowdown.
> >
> > If you're writing a MEX-file to interface with some other library or piece
> > of hardware, check out the LOADLIBRARY function as well as some of the
> > toolboxes and functions designed to help with that interfacing (the SERIAL
> > object in MATLAB or the Instrument Control Toolbox, Data Acquisition
> > Toolbox, etc.)
> >
> > http://www.mathworks.com/test-measurement/
> >
> > If you're writing a MEX-file to learn C or C++, perhaps you should focus on
> > learning C or C++ first, to avoid the added complexity imposed by the
> > interface between MATLAB and C or C++ that would make it harder for you to
> > learn.
> >
> > If you're writing a MEX-file to learn how to work with MEX-files, you may
> > want to start with a simpler problem.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> I have a Kmeans clustering function(not the inbuilt) in MATLAB which I need to convert to MEX as the data handled is too huge and MATLAB goes out of Memory with such huge data. Also there are a lot iterations to be done with the huge data. The data size is currently around 1.7million i.e. 2x1.7million double and later it could go up to 4 million. So I'm trying to work around the same algorithm in C (Mex).
It'l be really helpful if I could get an example of how things should be..

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Steven_Lord

Date: 26 Aug, 2013 14:35:18

Message: 18 of 20



"Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
news:kv9o2q$7iu$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kv8d9l$dgt$1@newscl01ah.mathworks.com>...

*snip*

> I have a Kmeans clustering function(not the inbuilt) in MATLAB which I
> need to convert to MEX as the data handled is too huge and MATLAB goes out
> of Memory with such huge data. Also there are a lot iterations to be done
> with the huge data. The data size is currently around 1.7million i.e.
> 2x1.7million double and later it could go up to 4 million. So I'm trying
> to work around the same algorithm in C (Mex).

If you're using the same convention as the KMEANS function in Statistics
Toolbox and your X is 2-by-1.7e6, that means you have two points in
1.7e6-dimensional space. Is that correct?

http://www.mathworks.com/help/stats/kmeans.html

"Rows of X correspond to points, columns correspond to variables."

If instead you have 1.7e6 points in 2-dimensional space, try using Parallel
Computing Toolbox to speed up the calculations and avoid the out of memory
error by using memory across multiple MATLAB sessions. The UseParallel
option will allow you to try this.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 28 Aug, 2013 10:33:08

Message: 19 of 20

"Steven_Lord" <slord@mathworks.com> wrote in message <kvfp36$fkp$1@newscl01ah.mathworks.com>...
>
>
> "Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
> news:kv9o2q$7iu$1@newscl01ah.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <kv8d9l$dgt$1@newscl01ah.mathworks.com>...
>
> *snip*
>
> > I have a Kmeans clustering function(not the inbuilt) in MATLAB which I
> > need to convert to MEX as the data handled is too huge and MATLAB goes out
> > of Memory with such huge data. Also there are a lot iterations to be done
> > with the huge data. The data size is currently around 1.7million i.e.
> > 2x1.7million double and later it could go up to 4 million. So I'm trying
> > to work around the same algorithm in C (Mex).
>
> If you're using the same convention as the KMEANS function in Statistics
> Toolbox and your X is 2-by-1.7e6, that means you have two points in
> 1.7e6-dimensional space. Is that correct?
>
> http://www.mathworks.com/help/stats/kmeans.html
>
> "Rows of X correspond to points, columns correspond to variables."
>
> If instead you have 1.7e6 points in 2-dimensional space, try using Parallel
> Computing Toolbox to speed up the calculations and avoid the out of memory
> error by using memory across multiple MATLAB sessions. The UseParallel
> option will allow you to try this.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I have never used Parallel Computing Toolbox. But i'm almost done with the Mex part.
Thanks James Tursa and Steve Lord..
Now only hick-ups i'm having is while indexing the array.. after using the convenient Matlab notation, linear indexing and specially when indexing a particular column as we would do in Matlab after using Find() are what I need to get accustomed to.

Subject: Mex function with more than 1 input and output multidimensional arrays

From: Hariprasad

Date: 28 Aug, 2013 10:42:08

Message: 20 of 20

"Hariprasad" wrote in message <kvkjl4$6ck$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <kvfp36$fkp$1@newscl01ah.mathworks.com>...
> >
> >
> > "Hariprasad " <sudharshan_hari@yahoo.co.in> wrote in message
> > news:kv9o2q$7iu$1@newscl01ah.mathworks.com...
> > > "Steven_Lord" <slord@mathworks.com> wrote in message
> > > <kv8d9l$dgt$1@newscl01ah.mathworks.com>...
> >
> > *snip*
> >
> > > I have a Kmeans clustering function(not the inbuilt) in MATLAB which I
> > > need to convert to MEX as the data handled is too huge and MATLAB goes out
> > > of Memory with such huge data. Also there are a lot iterations to be done
> > > with the huge data. The data size is currently around 1.7million i.e.
> > > 2x1.7million double and later it could go up to 4 million. So I'm trying
> > > to work around the same algorithm in C (Mex).
> >
> > If you're using the same convention as the KMEANS function in Statistics
> > Toolbox and your X is 2-by-1.7e6, that means you have two points in
> > 1.7e6-dimensional space. Is that correct?
> >
> > http://www.mathworks.com/help/stats/kmeans.html
> >
> > "Rows of X correspond to points, columns correspond to variables."
> >
> > If instead you have 1.7e6 points in 2-dimensional space, try using Parallel
> > Computing Toolbox to speed up the calculations and avoid the out of memory
> > error by using memory across multiple MATLAB sessions. The UseParallel
> > option will allow you to try this.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> I have never used Parallel Computing Toolbox. But i'm almost done with the Mex part.
> Thanks James Tursa and Steve Lord..
> Now only hick-ups i'm having is while indexing the array.. after using the convenient Matlab notation, linear indexing and specially when indexing a particular column as we would do in Matlab after using Find() are what I need to get accustomed to.
Like q = find(a(:,2) == b)
D = C(:,q);
something like this and also in a 3D array.

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