フィルターのクリア

How do you take the absolute value of only the complex number?

4 ビュー (過去 30 日間)
parslee
parslee 2021 年 11 月 12 日
編集済み: James Tursa 2021 年 11 月 12 日
I have a matrix of 128x256 filled with real and complex number; ex. -0.0115+0.0059i.
How do I take the absolute value of only the complex number so that it becomes -0.0115 + abs(0.0059i)?
I would like to apply this to all the cells in the matrix.
  1 件のコメント
James Tursa
James Tursa 2021 年 11 月 12 日
編集済み: James Tursa 2021 年 11 月 12 日
Did you really mean abs(0.0059i) with the i inside the abs( ) function as written? Or did you just mean abs(imag coeff)*i?

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

回答 (2 件)

Jon
Jon 2021 年 11 月 12 日
編集済み: Jon 2021 年 11 月 12 日
x = -0.0115+0.0059i
xnew = real(x) + abs(imag(x))*i
  1 件のコメント
Jon
Jon 2021 年 11 月 12 日
編集済み: Jon 2021 年 11 月 12 日
This will work for a m by n matrix too.
X = randn(128,256) + randn(128,256)*i;
Xnew = real(X) + abs(imag(X))*i

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


Walter Roberson
Walter Roberson 2021 年 11 月 12 日
NewArray = complex(real(YourArray), abs(imag(YourArray)));
or
NewArray = real(YourArray) + 1i .* abs(imag(YourArray));
Using complex() should be slightly more efficient.
There is a very slight difference in the results between the two: In the case where the imaginary components of the array are all 0, then the + 1i .* form would be adding 1i .* 0 which would all vanish and the end result in NewArray would be marked as entirely real-valued. But if you use complex() then even if the final result has all-zero in the imaginary component, the result will be marked as complex, and will have storage allocated for the complex part.
For most purposes, whether the all-zero imaginary part is stored or not makes no difference. But it can make a difference when you are calling library routines that are expecting a complex array, especially if you are expecting them to update the array "in-place"

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by