Error with find(X) statement

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?

 采纳的回答

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 个评论

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!
As always, my pleasure!
.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by