Find the indices of the imaginary element of the matrix

32 ビュー (過去 30 日間)
Hassan Alkomy
Hassan Alkomy 2021 年 6 月 8 日
回答済み: Hassan Alkomy 2021 年 6 月 8 日
Suppose I have a Matrix like this:
A=[1 2 3;
4+i 5 6-i;
7 8+i 9]
Now, I want to know if there is a way to find the indecies of the imaginary elements (by this I mean the complex numbers which have imaginary part). I am looking for an answer like that:
A(2,1)
A(2,3)
A(3,2)
or any other form the conveys the indecies information of the imaginary elements.
Thank you so much.

採用された回答

David Hill
David Hill 2021 年 6 月 8 日
Lidx=find(imag(A)~=0);
  2 件のコメント
David Hill
David Hill 2021 年 6 月 8 日
If your application will allow, I always perfer linear indexing.
Hassan Alkomy
Hassan Alkomy 2021 年 6 月 8 日
Thank you so much.

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

その他の回答 (3 件)

Joseph Cheng
Joseph Cheng 2021 年 6 月 8 日
you can do a comparison to the real(A) like
A=[1 2 3;
4+i 5 6-i;
7 8+i 9]
A =
1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i 4.0000 + 1.0000i 5.0000 + 0.0000i 6.0000 - 1.0000i 7.0000 + 0.0000i 8.0000 + 1.0000i 9.0000 + 0.0000i
[row col]=find(A~=real(A))
row = 3×1
2 3 2
col = 3×1
1 2 3
then with find you can get the row and column index values of the as real(A)~=A

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 8 日
% test matrix
A=[1 2 3;
4+i 5 6-i;
7 8+i 9]
% identify imaginary elements in A (+1 or -1)
B = imag(A)
% generate column vector of indices of imaginary elements in A
find(B~=0)
Output:
A =
1 + 0i 2 + 0i 3 + 0i
4 + 1i 5 + 0i 6 - 1i
7 + 0i 8 + 1i 9 + 0i
B =
0 0 0
1 0 -1
0 1 0
ans =
2
6
8

Hassan Alkomy
Hassan Alkomy 2021 年 6 月 8 日
Actually, the three solutions by David, Joseph and Scott give what i want. I chose David's answer because it is a one-line answer, but the rest of the solutions gives the same result.
Thank you all.

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by