create a complex array with fake imaginary part?

2 vues (au cours des 30 derniers jours)
Dehuan
Dehuan le 23 Déc 2014
Modifié(e) : Matt J le 25 Déc 2014
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

Réponses (3)

Matt J
Matt J le 23 Déc 2014
Modifié(e) : Matt J le 23 Déc 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 commentaires
Dehuan
Dehuan le 24 Déc 2014
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 le 24 Déc 2014
Modifié(e) : Matt J le 24 Déc 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.

Connectez-vous pour commenter.


Matt J
Matt J le 24 Déc 2014
Modifié(e) : Matt J le 24 Déc 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 le 24 Déc 2014
Modifié(e) : Matt J le 25 Déc 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.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by