Changing real part of complex number?

5 ビュー (過去 30 日間)
Danijel
Danijel 2017 年 10 月 10 日
編集済み: John D'Errico 2017 年 10 月 10 日
I have an array of compelx numbers. I'd like to change the real part of the 34th array member to zero. This code doesn't work:
real( my_complex_array(34) ) = 0;
What's the solution?

採用された回答

John D'Errico
John D'Errico 2017 年 10 月 10 日
編集済み: John D'Errico 2017 年 10 月 10 日
X = (1:35)' * (1+1i); % not being very creative here
Now, there are lots of ways to just kill the real part of only the 34'th element. This will work:
X(34) = X(34) - real(X(34));
Perhaps simpler, because it will not require the subtraction.
X(34) = 1i*imag(X(34));
Or,
X(34) = 0 + 1i*imag(X(34));
Another one, using complex.
X(34) = complex(0,imag(X(34));
In any event, did it work?
X(33:35)
ans =
33 + 33i
0 + 34i
35 + 35i
Of course it did.

その他の回答 (1 件)

KSSV
KSSV 2017 年 10 月 10 日
z = rand(50,1)+1i*rand(50,1) ;
z(34) = 0+1i*imag(z(34))

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by