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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by