How to create a ones array in mex ?

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2016-1-7
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 2016-1-7
编辑:James Tursa 2016-1-7
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by