How to create a ones array in mex ?

4 views (last 30 days)
Gauraang  Khurana
Gauraang Khurana on 5 Jan 2016
Edited: James Tursa on 7 Jan 2016
I am coding in c++ with mex interface. I need to create an array of ones of size 5 * 1. I am trying to use the mexCallMATLAB function but it gives me an error. Can someone please help me with this ?
Code : mexCallMATLAB(1,Ones,1,createOnes,"ones"); I was hoping that the output array will be displayed in Ones and the createOnes array is an array of size 5 * 1.
Error : Error using ones Size vector should be a row vector with real elements.
Please guide me. Thanks in advance
  9 Comments
Walter Roberson
Walter Roberson on 7 Jan 2016
I didn't know that the transpose would not actually be done in that case... interesting. Does the trick also work with A.'*B ?
James Tursa
James Tursa on 7 Jan 2016
Edited: James Tursa on 7 Jan 2016
Yes. The BLAS matrix multiply routines include character flag arguments for both A and B, 'N' for No transpose, 'T' for Transpose, and 'C' for Conjugate transpose. The MATLAB parser is smart enough to recognize this syntax at the m-file level and pass the flag instead of doing the physical transpose. Of course, I don't have access to MATLAB parser code, but this can easily be concluded based on timing results compared to equivalent mex code calling BLAS routines. Having said all that, I would remind you that MATLAB complex variables store the real part and imaginary part separately in memory, whereas the BLAS complex routines expect that data to be interleaved (ala Fortran). So rather than copy all the memory over to an interleaved state, call the complex matrix multiply, then copy the result to separated states, the multiply is accomplished with a series of real BLAS matrix multiply calls instead with appropriate factors for adding/subtracting the intermediate results. The factor (any real number) is an argument to the BLAS routine.
For LAPACK calls where one needs to call the complex routine, the memory copies into and out of an interleaved state must be done.
I don't know whether the MATLAB parser has gotten smart enough to recognize naked conjugates, however. E.g., something like this:
C = A * conj(B);
can be done at the BLAS calling level by simply passing a -1.0 as the factor argument to apply on the 2nd matrix imaginary part instead of a 1.0. I haven't checked lately to see if the MATLAB parser has gotten smart enough to do this or not. It used to physically do the conjugate and maybe still does.

Sign in to comment.

Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!