Complex double Array converts to double array after assignment

98 ビュー (過去 30 日間)
Vids deo
Vids deo 2016 年 9 月 27 日
コメント済み: Walter Roberson 2016 年 9 月 27 日
Hi Experts,
a = [1 2; 3 4];
b = [1 2; 3 4];
c = complex(a,b);
a1 = complex(a,0);
a1(1,1) = complex(abs(c(2,2)),0);
Above assignment somehow converts 'a1' to a DOUBLE array instead of COMPLEX double. I'm using the above method for assignment to a 3D array (S-parameter processing) which is facing the same issue in a code. Why would this be happening ??

採用された回答

Walter Roberson
Walter Roberson 2016 年 9 月 27 日
complex arrays whose real parts are all completely 0 are fragile; as soon as you do anything to them, the all-zero imaginary part will fall off.
Consider that if you have
a = [1+2i, 3+0i]
a(imag(a)>0) = 0
then this would be like a(1) = 0, giving you a matrix [0, 3+0i]. People now expect that to degrade to [0, 3] rather than retaining its complex character just because it was formerly complex. Therefore, the complex parts are checked for all-zero after every assignment.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 27 日
This is the way it is. It is considered a design feature.
If you want, you can use
if isreal(a1); a1 = complex(a1); end
But remember that if you then do a1+0 or a1*1 or anything with a1 other than assign all of it to another variable or pass it alone to a routine, then the complex part will promptly fall off again.

サインインしてコメントする。

その他の回答 (1 件)

Massimo Zanetti
Massimo Zanetti 2016 年 9 月 27 日
編集済み: Massimo Zanetti 2016 年 9 月 27 日
Sure it does, because a complex number with a NULL imaginary part is indeed real. Notice that in the call COMPLEX(a,0), you in fact sets the imaginary part to 0. If you want to create a complex number without imaginary part do this:
a1=complex(a);
This will give a1 as COMPLEX.
  1 件のコメント
Vids deo
Vids deo 2016 年 9 月 27 日
Hey Massimo,
The statement that you said only works with using complex(a) on complete matrix. It doesn't work the same way when used "element-wise" That is -
a1(1,1)=complex(a(2,1));
My operations are element-wise. And its screwing up the whole matrix to a real double (instead of complex double).
--Vidya

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by