Multiplying complex vector with its complex conjugate results in complex vector
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
DdeR
le 7 Mai 2021
Réponse apportée : Abhishek
le 2 Sep 2025 à 4:10
Hi,
Within a scrip I am multiplying a complex vector (added in dataset.mat) with its complex conjugate.
I.idmPSD_LT=idmLSD_LT.*conj(idmLSD_LT);
Now I expect my result to be real, as it is in for example
A=(1+2i)*(1-2i);
gives real 5. However the resulting vector is partly purely real(idx65:299968) and partly real and complex (and partly stuffed with NaN). For the complex part, Matlab displays x+0i. I can offcourse solve this by taking the real part of the vector, but I am wondering if anyone knows what causes this resulting vector to be complex. Does someone know this?
1 commentaire
Mathieu NOE
le 7 Mai 2021
hello
this is simply because your data vector contains NaN complex values - that will not give a "real" NaN output
remove first the NaN, do the product and then you get a real output
all the best
Réponse acceptée
Abhishek
le 2 Sep 2025 à 4:10
The output is complex because the input vector “idmLSD_LT” contains NaN values. Any calculation involving a complex NaN (of the form “NaN + NaNi”) will produce a complex NaN as a result. Such entries should be removed before performing the calculation.
In MATLAB, you can remove the NaNs with the help of “isnan()” function and “~” operator. Create a new vector that would only contain non-NaN elements.
idmLSD_LT_clean = idmLSD_LT(~isnan(idmLSD_LT));
Then perform the operation on the new vector:
I.idmPSD_LT=idmLSD_LT_clean.*conj(idmLSD_LT_clean);
I tried this approach on the data posted in the question on MATLAB R2025a and got the expected results.

You can refer to the official documentation by MATLAB on “isnan()” function: https://mathworks.com/help/releases/R2025a/matlab/ref/double.isnan.html
I hope this helps.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!