Multiplying complex vector with its complex conjugate results in complex vector

28 次查看(过去 30 天)
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 个评论
Mathieu NOE
Mathieu NOE 2021-5-7
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

请先登录,再进行评论。

采纳的回答

Abhishek
Abhishek 2025-9-2,4:10
Hello @DdeR,
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 ofisnan()” 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 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by