create a complex array with fake imaginary part?

Hi, everyone,
I want to create a complex array but with real values. I tried things like b=(complex(a)), but it turns out that imaginary part does not exist. Then I pass the array to a mex function, and the mex runs a mxGetPi and fails because the imaginary part is not allocated, then it crashes.
Is there a way to fake a complex array in matlab with imaginary part allocated?
Thanks
Dehuan

Answers (3)

Matt J
Matt J on 23 Dec 2014
Edited: Matt J on 23 Dec 2014
The only thing that comes to mind is to add a hopefully negligible non-zero imaginary part to one of the elements,
a(k)=a(k)+eps(a(k))*i;
Note that only one imaginary element in the array needs to be non-zero in order for mxGetPi to see an imaginary part. You might be able to be strategic about your choice of k, depending on what your application is doing.

7 Comments

I tried this, actually, but when I cast the array to integer, then the small imaginary part disappears. Is there's a way to solve/hack it?
There is no complex integer type in MATLAB. So pass in the real integer part to your code, and add a zero imaginary part there. What is the problem? If you will sometimes have a non-zero imaginary part, then separate the real and imaginary parts in MATLAB, and pass them in separately, then recombine them in C.
There are complex integer types, e.g.,
>> a=uint8(1+1i); whos a
Name Size Bytes Class Attributes
a 1x1 2 uint8 complex
but setting the imaginary part to zero will make MATLAB strip away the complex Attribute. I guess you would indeed have to add the imaginary array inside the MEX file.
Dehuan Commented:
The C function was expecting interleaved real/imaginary in int16 format.
And the mex wrapper is written in such a way that it accepts complex int16s and interleave the real and imaginary part in the mex wrapper.
Although I understand the best way to do it is to pass double complexes to mex and cast and interleave there, or cast and interleave in matlab before passing data to mex(personally I prefer the latter), I'm seeking a way to to use that mex function with some real-only data without changing the mex code.
I'm seeking a way to to use that mex function with some real-only data without changing the mex code.
I don't think you'll find it, unless the operation has special properties like linearity ( see my other answer ).
I'm not sure why you're reluctant to modify the mex code, however. It should just require a few lines at the beginning of the code to add a zero imaginary part that mxGetPi can see.
The reason is as simple as "One can not make his/her boss to change his/her code because he/she want to use it in a hacked/ugly way."
Matt J
Matt J on 24 Dec 2014
Edited: Matt J on 24 Dec 2014
"One can not make his/her boss to change his/her code because he/she want to use it in a hacked/ugly way."
Your boss' code is already ugly if it can't handle strictly real input data. Imagine an fft() routine that could only handle a signal with a non-zero imaginary part.
I would find a diplomatic way to tell him/her that, and offer to upgrade the code as we've described.

Sign in to comment.

Matt J
Matt J on 24 Dec 2014
Edited: Matt J on 24 Dec 2014
This might be wishful thinking, but if the operation f() to be performed by the MEX is linear, you could leverage that,
f(a) = f(a+b*i)+f(a-b*i)
where b is any fake non-zero imaginary data that you wish to add.
Matt J
Matt J on 24 Dec 2014
Edited: Matt J on 25 Dec 2014
You could try creating your own mex function that appends a zero imaginary part to a real int16 input. If you use mxCreateNumericArray with the ComplexFlag argument set to true, I think there's a chance that it will let you allocate a zero imaginary part.
You would need to use the conversion tool in the very last step, just before the input needs to be fed to your boss' mex code.

Categories

Products

Tags

Asked:

on 23 Dec 2014

Edited:

on 25 Dec 2014

Community Treasure Hunt

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

Start Hunting!