Remove Terms in Complex Valued Data That Has Only Real or Imaginary Part

조회 수: 11 (최근 30일)
MarshallSc
MarshallSc 2022년 6월 13일
댓글: Voss 2022년 6월 13일
I have a complex numeric array (file attached) that some of its terms have only real or imaginary part, and the rest have both the real and imaginary part. How can I remove the terms in the array that has only one term (real or imaginary) so that the terms that has both real and imaginary are left in the array? For example, for the first 8 rows of the data, I want to remove the first 7 terms:
0.000 + 0.254i
0.000 + 0.317i
0.000 + 0.298i
-0.0467 + 0.000i
0.000 + 0.317i
0.000 + 0.401i
0.000 + 0.437i
-0.0453 + 0.0029i % only this term is needed
Thank you!

채택된 답변

Voss
Voss 2022년 6월 13일
편집: Voss 2022년 6월 13일
load LR
disp(LR);
0.0000 + 0.2545i 0.0000 + 0.3173i 0.0000 + 0.2984i -0.0468 + 0.0000i 0.0000 + 0.3173i 0.0000 + 0.4014i 0.0000 + 0.4377i -0.0454 + 0.0029i 0.0000 + 0.2984i 0.0000 + 0.4377i -0.1659 + 1.8998i -0.0399 + 0.0053i -0.0468 + 0.0000i -0.0454 + 0.0029i -0.0399 + 0.0053i -0.0347 + 0.0000i 0.0000 + 0.3649i 0.0000 + 0.6330i 0.0000 + 0.3457i -0.0406 + 0.0000i 0.0000 + 0.6330i 0.0000 + 1.5188i 0.0000 + 0.4850i -0.0389 + 0.0000i 0.0000 + 0.3457i 0.0000 + 0.4850i -0.2540 + 1.6553i 0.0000 + 0.0856i -0.0406 + 0.0000i -0.0389 + 0.0000i 0.0000 + 0.0856i -0.0641 + 0.0000i -0.2924 + 0.0069i -0.2233 + 0.0000i -0.2486 + 0.0000i -0.0379 + 0.0000i -0.2233 + 0.0000i -0.2693 + 0.0000i -0.2948 + 0.0000i -0.2597 + 0.0000i -0.2486 + 0.0000i -0.2948 + 0.0000i -24.8645 + 0.0000i -0.0337 + 0.0000i -0.0379 + 0.0000i -0.2597 + 0.0000i -0.0337 + 0.0000i -0.0339 + 0.0000i -0.0140 + 0.0000i -0.0133 + 0.0000i 0.0000 + 0.0057i -0.0359 + 0.0000i -0.0133 + 0.0000i -0.0529 + 0.0000i -0.0624 + 0.0000i -0.0359 + 0.0000i 0.0000 + 0.0057i -0.0624 + 0.0000i -0.0346 + 0.1377i -0.0336 + 0.0000i -0.0359 + 0.0000i -0.0359 + 0.0000i -0.0336 + 0.0000i -0.0339 + 0.0000i
One way:
% remove elements of LR whose real part is 0 or imaginary part is 0
LR(real(LR) == 0 | imag(LR) == 0) = [];
disp(LR);
-0.0454 + 0.0029i -0.1659 + 1.8998i -0.0399 + 0.0053i -0.0454 + 0.0029i -0.0399 + 0.0053i -0.2540 + 1.6553i -0.2924 + 0.0069i -0.0346 + 0.1377i
Another way:
load LR
% keep elements of LR whose real part is non-zero and imaginary part is non-zero
LR = LR(real(LR) ~= 0 & imag(LR) ~= 0);
disp(LR)
-0.0454 + 0.0029i -0.1659 + 1.8998i -0.0399 + 0.0053i -0.0454 + 0.0029i -0.0399 + 0.0053i -0.2540 + 1.6553i -0.2924 + 0.0069i -0.0346 + 0.1377i
load LR
% same but more concise:
LR = LR(real(LR) & imag(LR));
disp(LR)
-0.0454 + 0.0029i -0.1659 + 1.8998i -0.0399 + 0.0053i -0.0454 + 0.0029i -0.0399 + 0.0053i -0.2540 + 1.6553i -0.2924 + 0.0069i -0.0346 + 0.1377i

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by