필터 지우기
필터 지우기

Complex matrix , make certain elements of real and imaginary as zero in a single line without storage

조회 수: 14 (최근 30일)
Suppose I have the following complex matrix
s = rng;
A = complex(randn(5,5),randn(5,5));
T = 0.15
How do I make both the real part corresponding to less than T equal to zero and how do I make imaginary part less than T equal to zero in the same step It is like making the following in one single line
1. realA = real(A);
2. imagA = imag(A);
3. realA(realA<T) = 0
4. imagA(imagA<T) = 0
5. A = complex(realA,imagA)
How do I convert the five steps above in a single line only using A , and no storing of real part of A and imaginary part of A in realA and imagA respectively.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 8월 29일
A = complex( real(A) .* (real(A) >= T), imag(A) .* (imag(A) >= T));
However, this will fail for real or complex part equal to -infinity, creating NaN in those locations, because 0 (false) times infinity is NaN instead of 0.
  댓글 수: 11
Walter Roberson
Walter Roberson 2018년 8월 30일
If your goal is FPGA then give up on writing as single statement and just do in-place for loop.
Walter Roberson
Walter Roberson 2018년 8월 31일
for k=1:length(A)
Ar = real(A);
Ai = imag(A);
if Ar < T; Ar = 0; end
if Ai < T; Ai = 0; end
A(K) = complex(Ar,Ai);
end
Extra storage: minimal.
Multiplications: none.
If you want to improve performance at the expense of additional die space, have your compiler do automatic loop unrolling.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by