Why do logical operators do not work with the eigenvalues of this matrix?

1 回表示 (過去 30 日間)
Vipul Kumar
Vipul Kumar 2021 年 4 月 30 日
回答済み: Steven Lord 2021 年 4 月 30 日
I am trying to find index of eigenvalues of a matrix which are equal to 1. But for some reason the '==' or 'ismember' don't work to recognize that. Can someone look at this code and suggest, what is wrong with this?
TM = [0.1,0.2,0.7; 0.5,0.2,0.3;0.1,0.1,0.8];
[V,D] = eig(TM');
V
D
vec = sum(D);
for n1 = 1:length(vec)
if (vec(n1) == 1)
index_for_pop = n1;
end
end
index_for_pop
===output
V =
-0.2022 -0.8100 -0.4938
-0.1711 0.3162 -0.3162
-0.9643 0.4938 0.8100
D =
1.0000 0 0
0 -0.1562 0
0 0 0.2562
Undefined function or variable 'index_for_pop'.
Error in trial2 (line 14)
index_for_pop

採用された回答

Christine Tobler
Christine Tobler 2021 年 4 月 30 日
There's a small round-off error on the eigenvalue 1:
>> TM = [0.1,0.2,0.7; 0.5,0.2,0.3;0.1,0.1,0.8];
[V,D] = eig(TM', 'vector');
D(1)
ans =
1.0000
>> D(1) - 1
ans =
4.4409e-16
It's usually not a good idea in numerical computations to do an "==" type comparison, better to use abs(D(1) - 1) < 1e-14, for example.

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 4 月 30 日
The eigenvalue is not exactly equal to 1. It is close enough to 1 that it is displayed as 1 in the default display format.
You might want to call eig with the 'vector' input so it returns your eigenvalues as a vector rather than a diagonal matrix.
Use ismembertol or the technique shown in the last block of code in this Answers post.

カテゴリ

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