Error with find(X) statement

3 ビュー (過去 30 日間)
Anu Raghunathan
Anu Raghunathan 2021 年 7 月 28 日
コメント済み: Star Strider 2021 年 7 月 28 日
I'm trying to run a simple calculation to find the non-trivial columns of a unitary matrix. All the unitaries in this case are of the form:
[ 1 0 0 0,
0 a b 0,
0 c d 0,
0 0 0 1]
where a b c d form the 'non-trivial columns', in whatever columns.
Which is to say, if you sum the matrix either column wise or row wise, the sum must always equal 1, or be the non-trivial columns I'm trying to find. So the way I'm doing it is:
states=find(sum(UNITARY_MATRIX)~=1.0)
Which generally outputs something like:
Unitary number: 5
1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.8165 + 0.0000i 0.0000 + 0.0000i -0.0000 - 0.5774i
0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.0000 + 0.5774i 0.0000 + 0.0000i -0.8165 + 0.0000i
sum of column values
1.0000 + 0.0000i 0.8165 + 0.5774i 1.0000 + 0.0000i -0.8165 - 0.5774i
Non trivial columns:
2 4
This is working very well for all the unitaries I'm checking, except for the following: (this is the output in the terminal)
Unitary number: 6
1.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i
0.0000 + 0.0000i 1.0000 - 0.0000i 0.0000 + 0.0000i -0.0000 - 0.0000i
-0.0000 + 0.0000i 0.0000 - 0.0000i 0.7071 - 0.0000i -0.7071 - 0.0000i
-0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 0.7071i 0.0000 - 0.7071i
sum of column values
1.0000 + 0.0000i 1.0000 + 0.0000i 0.7071 - 0.7071i -0.7071 - 0.7071i
Non trivial columns:
2 3 4
Any idea why this is happening? This is the only matrix this is happening with. Since clearly the non-trivial columns should be 3 and 4. Is this to do with how find(X) reads complex numbers?

採用された回答

Star Strider
Star Strider 2021 年 7 月 28 日
Taking a wild guess, since the test is:
states=find(sum(UNITARY_MATRIX)~=1.0)
it is likely encountering floating-point approximation error, since the sum may not be exactly equal to 1.0, the criterion for the test.
Perhaps something like:
states=find(abs(1.0 - sum(UNITARY_MATRIX)) <= 1E-8)
or something similar (incorporating a range of some sort for the test) would work.
If this does not address what you want to do, I will delete my Answer.
.
  2 件のコメント
Anu Raghunathan
Anu Raghunathan 2021 年 7 月 28 日
Great that works perfectly! Only thing is I'm looking for elements where the sum is not equal to 1.0, so I just amended your code to:
states=find(abs(1.0 - sum(UNITARY_MATRIX)) > 1E-8);
And it works perfectly!! Thanks!
Star Strider
Star Strider 2021 年 7 月 28 日
As always, my pleasure!
.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by