double mxarray to int mxarray in mex file

  • Follow


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:













7/22/2012 1:09:56 PM


Reply: