How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
Thanks
|
|
0
|
|
|
|
Reply
|
Sebastian
|
4/21/2010 2:59:07 AM |
|
"Sebastian " <quit.spaseba@email.google> wrote in message <hqlplr$h8f$1@fred.mathworks.com>...
> How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
>
> Thanks
mxArray * mx = your double array.
double *pr;
int *ip;
mwSize i, n;
pr = mxGetPr(mx);
n = mxGetNumberOfElements(mx);
ip = mxMalloc(n * sizeof(*ip));
for( i=0; i<n; i++ ) {
ip[i] = pri[i];
}
// use the ip array
mxFree(ip);
James Tursa
|
|
0
|
|
|
|
Reply
|
James
|
4/21/2010 4:41:04 AM
|
|
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hqlvl0$n7s$1@fred.mathworks.com>...
> "Sebastian " <quit.spaseba@email.google> wrote in message <hqlplr$h8f$1@fred.mathworks.com>...
> > How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
> >
> > Thanks
>
> mxArray * mx = your double array.
> double *pr;
> int *ip;
> mwSize i, n;
> pr = mxGetPr(mx);
> n = mxGetNumberOfElements(mx);
> ip = mxMalloc(n * sizeof(*ip));
> for( i=0; i<n; i++ ) {
> ip[i] = pri[i];
> }
> // use the ip array
> mxFree(ip);
>
> James Tursa
The point being that you cannot cast one to the other, you need to copy, converting each array member one at a time.
|
|
0
|
|
|
|
Reply
|
Steve
|
4/21/2010 10:13:08 AM
|
|
"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hqmj3j$q9i$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hqlvl0$n7s$1@fred.mathworks.com>...
> > "Sebastian " <quit.spaseba@email.google> wrote in message <hqlplr$h8f$1@fred.mathworks.com>...
> > > How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
> > >
> > > Thanks
> >
> > mxArray * mx = your double array.
> > double *pr;
> > int *ip;
> > mwSize i, n;
> > pr = mxGetPr(mx);
> > n = mxGetNumberOfElements(mx);
> > ip = mxMalloc(n * sizeof(*ip));
> > for( i=0; i<n; i++ ) {
> > ip[i] = pri[i];
> > }
> > // use the ip array
> > mxFree(ip);
> >
> > James Tursa
>
> The point being that you cannot cast one to the other, you need to copy, converting each array member one at a time.
Thanks both. What is the faster way? Like is indicated by James Tursa in the mex file, or is better use int32(DoubleMatrix) in the MatLab enviroment and after call mex function.
I have call a function of a library with int arguments, but in MatLab i would like use double matrix (default type).
|
|
0
|
|
|
|
Reply
|
Sebastian
|
4/21/2010 3:15:07 PM
|
|
"Sebastian " <quit.spaseba@email.google> wrote in message <hqn4pr$dhi$1@fred.mathworks.com>...
> "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hqmj3j$q9i$1@fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hqlvl0$n7s$1@fred.mathworks.com>...
> > > "Sebastian " <quit.spaseba@email.google> wrote in message <hqlplr$h8f$1@fred.mathworks.com>...
> > > > How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
> > > >
> > > > Thanks
> > >
> > > mxArray * mx = your double array.
> > > double *pr;
> > > int *ip;
> > > mwSize i, n;
> > > pr = mxGetPr(mx);
> > > n = mxGetNumberOfElements(mx);
> > > ip = mxMalloc(n * sizeof(*ip));
> > > for( i=0; i<n; i++ ) {
> > > ip[i] = pri[i];
> > > }
> > > // use the ip array
> > > mxFree(ip);
> > >
> > > James Tursa
> >
> > The point being that you cannot cast one to the other, you need to copy, converting each array member one at a time.
>
> Thanks both. What is the faster way? Like is indicated by James Tursa in the mex file, or is better use int32(DoubleMatrix) in the MatLab enviroment and after call mex function.
> I have call a function of a library with int arguments, but in MatLab i would like use double matrix (default type).
Doing it at the MATLAB level you will get a slight amount of increased overhead associated with creating an additional mxArray structure, whereas the mex file can avoid this. But other than that the speed of the actual conversion of double to int is going to be the same and I doubt you would notice the difference in timing. The real answer may depend on what this other routine does with this int array. If it treats the int array as read-only, then either solution will work acceptably for you. But if this other routine modifies the int array contents, then you should use the mex approach I list and do the conversion inside the mex routine.
James Tursa
|
|
0
|
|
|
|
Reply
|
James
|
4/21/2010 4:23:04 PM
|
|
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hqn8p8$27n$1@fred.mathworks.com>...
> "Sebastian " <quit.spaseba@email.google> wrote in message <hqn4pr$dhi$1@fred.mathworks.com>...
> > "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hqmj3j$q9i$1@fred.mathworks.com>...
> > > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hqlvl0$n7s$1@fred.mathworks.com>...
> > > > "Sebastian " <quit.spaseba@email.google> wrote in message <hqlplr$h8f$1@fred.mathworks.com>...
> > > > > How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way?
> > > > >
> > > > > Thanks
> > > >
> > > > mxArray * mx = your double array.
> > > > double *pr;
> > > > int *ip;
> > > > mwSize i, n;
> > > > pr = mxGetPr(mx);
> > > > n = mxGetNumberOfElements(mx);
> > > > ip = mxMalloc(n * sizeof(*ip));
> > > > for( i=0; i<n; i++ ) {
> > > > ip[i] = pri[i];
> > > > }
> > > > // use the ip array
> > > > mxFree(ip);
> > > >
> > > > James Tursa
> > >
> > > The point being that you cannot cast one to the other, you need to copy, converting each array member one at a time.
> >
> > Thanks both. What is the faster way? Like is indicated by James Tursa in the mex file, or is better use int32(DoubleMatrix) in the MatLab enviroment and after call mex function.
> > I have call a function of a library with int arguments, but in MatLab i would like use double matrix (default type).
>
> Doing it at the MATLAB level you will get a slight amount of increased overhead associated with creating an additional mxArray structure, whereas the mex file can avoid this. But other than that the speed of the actual conversion of double to int is going to be the same and I doubt you would notice the difference in timing. The real answer may depend on what this other routine does with this int array. If it treats the int array as read-only, then either solution will work acceptably for you. But if this other routine modifies the int array contents, then you should use the mex approach I list and do the conversion inside the mex routine.
>
> James Tursa
Thanks James, very useful and precise.
|
|
0
|
|
|
|
Reply
|
Sebastian
|
4/21/2010 5:51:04 PM
|
|
|
5 Replies
506 Views
(page loaded in 0.078 seconds)
Similiar Articles: double mxarray to int mxarray in mex file - comp.soft-sys.matlab ...How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way? Thanks ... argument of type "double" is incompatible with parameter of type ...Convert C image to mxArray - comp.soft-sys.matlab double mxarray to int mxarray in mex file - comp.soft-sys.matlab ... argument of type "double" is incompatible with ... int32 to double - comp.soft-sys.matlabdouble mxarray to int mxarray in mex file - comp.soft-sys.matlab ... 1) You cannot mix mxArray memory with ... How to return a 2D double array in Mex - comp.soft-sys.matlab ... Convert C array into mxArray Type - comp.soft-sys.matlab ...Convert C image to mxArray - comp.soft-sys.matlab double mxarray to int mxarray in mex file ... The array has type type Matrix is array(Positive ... but this cannot be ... mexCallMATLAB : mxArray input and output problem - comp.soft-sys ...... The mex file : void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs ... mexCallMATLAB but I ... from * mxArray to double and double a * mxArray ... What() is:bad allocation Error in mex file - comp.soft-sys.matlab ...so i have a simple mex file mextest.cpp: #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int ... How to return a 2D double array in Mex - comp.soft-sys ... Convert int to double then back to int? - comp.lang.c++.moderated ...double mxarray to int mxarray in mex file - comp.soft-sys.matlab ... How i can cast double mxarray to int mxarray ... If it treats the int array as read-only, then either ... Mex compiling problem - comp.soft-sys.matlab... without specifying specialization parameters in function mexFunction(int,mxArray_tag ... I include the mex.h file, which includes the matrix.h file. ... Use dll in C Mex S ... create a struct in mexFunction - comp.soft-sys.math.scilab ...hi, in mexFunction(int nlhs,mxArray ... 10)); mxArray *mat = mxCreateDoubleMatrix(1,2,mxREAL); double *pt = mxGetPr ... MATLAB MEX-files - CodeProject This ... How to return a 2D double array in Mex - comp.soft-sys.matlab ...... mex file - comp.soft-sys.matlab ... How to return a 2D double array in Mex ... 2D double array in Mex - comp.soft-sys.matlab ... 1) You cannot mix mxArray ... 2d int array in mex ... double mxarray to int mxarray in mex file - comp.soft-sys.matlab ...How i can cast double mxarray to int mxarray in a C++ mex file? What is the easier way? Thanks ... Structures with Mex-Files - GNU Octave - The GNU Operating SystemA.2.5 Structures with Mex-Files. The basic ... int key_num, mxArray *val); A difference between the oct-file interface to structures and the mex-file version is that the ... 7/22/2012 1:09:56 PM
|