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
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
2025년 9월 2일 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!