필터 지우기
필터 지우기

Expanding a complex element to a matrix

조회 수: 2 (최근 30일)
And Meng
And Meng 2017년 1월 17일
댓글: And Meng 2017년 1월 17일
Hi,
I have a complex matrix and I would like to convert the matrix to the real and imaginary components without changing the location. Say the complex matrix is
[A11 A12;
A21 A22]
each Aij = Gij + 1j* Bij. After expanding the matrix, I would get my desire matrix as
[[G11 -B11;B11 G11] [G12 -B12;B12 G12] ;
[G21 -B21;B21 G21] [G22 -B22;B22 G22]]
In the code below, the variable B indicates what I want to achieve, but I want to do it more efficiently than via a for loop and the i*2 indicator.
clear
clc
nb=2;
A=complex(magic(nb),1./magic(nb));
% tic
Ar=real(A);
Ai=imag(A);
B=zeros(2*nb,2*nb);
for i=1:nb
for j=1:nb
B(2*i-1,2*j-1)= Ar(i,j);
B(2*i-1,2*j) =-Ai(i,j);
B(2*i,2*j-1) = Ai(i,j);
B(2*i,2*j) = Ar(i,j);
end
end
% toc
Any advice would be much appreciated. Thanks for reading.

채택된 답변

Matt J
Matt J 2017년 1월 17일
Ar=real(A);
Ai=imag(A);
B=zeros(2*nb,2*nb);
B(1:2:end,1:2:end)=Ar;
B(1:2:end,1:2:end)=Ar;
B(1:2:end,2:2:end)=-Ai;
B(2:2:end,1:2:end)=Ai;
  댓글 수: 2
Stephen23
Stephen23 2017년 1월 17일
+1 nice solution.
And Meng
And Meng 2017년 1월 17일
Works great after changing a small typo on your solution.
B(1:2:end,1:2:end)=Ar;
B(1:2:end,1:2:end)=Ar;
should be
B(1:2:end,1:2:end)=Ar;
B(2:2:end,2:2:end)=Ar;
Thank you very much Matt!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by