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