R中的特征向量复数非对称矩阵不同于Matlab:如何解决这个问题? [英] eigenvectors complex nonsymmetric matrix in R different from Matlab: how to solve the this?
问题描述
我想计算R中复杂矩阵的特征值. 比较在MATLAB中获得的值,我没有从R中从完全相同的矩阵进行计算而获得的本征值相同.
I want to compute the eigenvalues of a complex matrix in R. Comparing the values obtained in MATLAB, I don't get the same eigenvalues obtained in R computing it from the exact same matrix.
在R中,我使用了eigen()
.在MATLAB中,使用eig()
或eigs()
(两个函数都给出相同的特征值,但不同于R的特征值).
In R, I used eigen()
. In MATLAB used eig()
or eigs()
(both functions give out the same eigenvalues but different to the R ones).
对于真实矩阵,R和MATLAB是一致的. 如何在R中获得与MATLAB相同的结果?
For real matrices, R and MATLAB are consistent. How to get in R the same result of MATLAB ?
Matlab 实矩阵中的示例:
Example in Matlab real matrix:
squeeze(Sigma_X(:,:,h+1)) %real matrix 10x10
ans =
1.0e+03 *
Columns 1 through 6
5.8706 5.9966 6.1225 6.2484 6.3744 6.5003
5.9966 6.1260 6.2554 6.3849 6.5143 6.6438
6.1225 6.2554 6.3884 6.5213 6.6543 6.7872
6.2484 6.3849 6.5213 6.6578 6.7942 6.9306
6.3744 6.5143 6.6543 6.7942 6.9341 7.0741
6.5003 6.6438 6.7872 6.9306 7.0741 7.2175
6.6263 6.7732 6.9201 7.0671 7.2140 7.3609
6.7522 6.9026 7.0531 7.2035 7.3539 7.5044
6.8782 7.0321 7.1860 7.3399 7.4939 7.6478
7.0041 7.1615 7.3190 7.4764 7.6338 7.7912
Columns 7 through 10
6.6263 6.7522 6.8782 7.0041
6.7732 6.9026 7.0321 7.1615
6.9201 7.0531 7.1860 7.3190
7.0671 7.2035 7.3399 7.4764
7.2140 7.3539 7.4939 7.6338
7.3609 7.5044 7.6478 7.7912
7.5079 7.6548 7.8017 7.9487
7.6548 7.8052 7.9557 8.1061
7.8017 7.9557 8.1096 8.2635
7.9487 8.1061 8.2635 8.4210
opt.disp = 0;
[P, D] = eigs(squeeze(Sigma_X(:,:,h+1)),q,'LM',opt) %compute the eigenvalues and eigenvectors
P =
-0.2872 0.5128
-0.2936 0.4029
-0.2999 0.2930
-0.3062 0.1830
-0.3125 0.0731
-0.3189 -0.0368
-0.3252 -0.1467
-0.3315 -0.2566
-0.3379 -0.3665
-0.3442 -0.4764
D =
1.0e+04 *
7.0984 0
0 0.0054
R 实数矩阵中的示例:
drop(Sigma_X[,,h+1]) #Same real matrix as before, 10x10
Columns 1 through 5
5870.610+0i 5996.552+0i 6122.495+0i 6248.438+0i 6374.381+0i
5996.552+0i 6125.994+0i 6255.435+0i 6384.876+0i 6514.317+0i
6122.495+0i 6255.435+0i 6388.375+0i 6521.314+0i 6654.254+0i
6248.438+0i 6384.876+0i 6521.314+0i 6657.752+0i 6794.190+0i
6374.381+0i 6514.317+0i 6654.254+0i 6794.190+0i 6934.127+0i
6500.324+0i 6643.759+0i 6787.194+0i 6930.629+0i 7074.063+0i
6626.267+0i 6773.200+0i 6920.133+0i 7067.067+0i 7214.000+0i
6752.210+0i 6902.641+0i 7053.073+0i 7203.505+0i 7353.937+0i
6878.152+0i 7032.083+0i 7186.013+0i 7339.943+0i 7493.873+0i
7004.095+0i 7161.524+0i 7318.952+0i 7476.381+0i 7633.810+0i
Columns 6 through 10
6500.324+0i 6626.267+0i 6752.210+0i 6878.152+0i 7004.095+0i
6643.759+0i 6773.200+0i 6902.641+0i 7032.083+0i 7161.524+0i
6787.194+0i 6920.133+0i 7053.073+0i 7186.013+0i 7318.952+0i
6930.629+0i 7067.067+0i 7203.505+0i 7339.943+0i 7476.381+0i
7074.063+0i 7214.000+0i 7353.937+0i 7493.873+0i 7633.810+0i
7217.498+0i 7360.933+0i 7504.368+0i 7647.803+0i 7791.238+0i
7360.933+0i 7507.867+0i 7654.800+0i 7801.733+0i 7948.667+0i
7504.368+0i 7654.800+0i 7805.232+0i 7955.663+0i 8106.095+0i
7647.803+0i 7801.733+0i 7955.663+0i 8109.594+0i 8263.524+0i
7791.238+0i 7948.667+0i 8106.095+0i 8263.524+0i 8420.952+0i
Decomp <- eigen(drop(Sigma_X[,,h+1])) #frequency 0
DD <- diag(Decomp$values[1:q])
PP <- Decomp$vectors[,1:q]
PP
[1,] -0.2872322+0i 0.5127886+0i
[2,] -0.2935595+0i 0.4028742+0i
[3,] -0.2998868+0i 0.2929598+0i
[4,] -0.3062141+0i 0.1830454+0i
[5,] -0.3125415+0i 0.0731310+0i
[6,] -0.3188688+0i -0.0367834+0i
[7,] -0.3251961+0i -0.1466978+0i
[8,] -0.3315234+0i -0.2566122+0i
[9,] -0.3378507+0i -0.3665266+0i
[10,] -0.3441780+0i -0.4764410+0i
DD
[1,] 70983.65 0.00000
[2,] 0.00 54.34878
您可以看到,当矩阵是实数时,MATLAB会给您返回相同的特征向量和特征值.
AS YOU CAN SEE, WHEN THE MATRIX IS REAL, MATLAB AND R GIVE YOU BACK THE SAME EIGENVECTORS AND EIGENVALUES.
让我们使用复杂的矩阵尝试相同的代码.
LET'S TRY THE SAME CODE BUT WITH A COMPLEX MATRIX.
Matlab 复杂矩阵中的示例:
Example in Matlab complex matrix:
j=1
squeeze(Sigma_X(:,:,j))
ans =
1.0e+02 *
Columns 1 through 5
3.4601+0.0000i 3.5075-0.0304i 3.5548-0.0607i 3.6022-0.0911i 3.6496-0.1215i
3.5075+0.0304i 3.5562+0.0000i 3.6049-0.0304i 3.6535-0.0607i 3.7022-0.0911i
3.5548+0.0607i 3.6049+0.0304i 3.6549+0.0000i 3.7049-0.0304i 3.7549- 0.0607i
3.6022+0.0911i 3.6535+0.0607i 3.7049+0.0304i 3.7562+0.0000i 3.8075- 0.0304i
3.6496+0.1215i 3.7022+0.0911i 3.7549+0.0607i 3.8075+0.0304i 3.8602+ 0.0000i
3.6970+0.1518i 3.7509+0.1215i 3.8049+0.0911i 3.8588+0.0607i 3.9128+ 0.0304i
3.7444+0.1822i 3.7996+0.1518i 3.8549+0.1215i 3.9102+0.0911i 3.9654+ 0.0607i
3.7917+0.2126i 3.8483+0.1822i 3.9049+0.1518i 3.9615+0.1215i 4.0181+ 0.0911i
3.8391+0.2429i 3.8970+0.2126i 3.9549+0.1822i 4.0128+0.1518i 4.0707+ 0.1215i
3.8865+0.2733i 3.9457+0.2429i 4.0049+0.2126i 4.0641+0.1822i 4.1234+ 0.1518i
Columns 6 through 10
3.6970-0.1518i 3.7444-0.1822i 3.7917-0.2126i 3.8391-0.2429i 3.8865- 0.2733i
3.7509-0.1215i 3.7996-0.1518i 3.8483-0.1822i 3.8970-0.2126i 3.9457- 0.2429i
3.8049-0.0911i 3.8549-0.1215i 3.9049-0.1518i 3.9549-0.1822i 4.0049- 0.2126i
3.8588-0.0607i 3.9102-0.0911i 3.9615-0.1215i 4.0128-0.1518i 4.0641- 0.1822i
3.9128-0.0304i 3.9654-0.0607i 4.0181-0.0911i 4.0707-0.1215i 4.1234- 0.1518i
3.9668+0.0000i 4.0207-0.0304i 4.0747-0.0607i 4.1286-0.0911i 4.1826- 0.1215i
4.0207+0.0304i 4.0760+0.0000i 4.1313-0.0304i 4.1865-0.0607i 4.2418- 0.0911i
4.0747+0.0607i 4.1313+0.0304i 4.1878+0.0000i 4.2444-0.0304i 4.3010- 0.0607i
4.1286+0.0911i 4.1865+0.0607i 4.2444+0.0304i 4.3023+0.0000i 4.3602- 0.0304i
4.1826+0.1215i 4.2418+0.0911i 4.3010+0.0607i 4.3602+0.0304i 4.4195+ 0.0000i
[P, D] = eigs(squeeze(Sigma_X(:,:,j)),q,'LM',opt);
P =
-0.1206 - 0.2711i 0.0471 + 0.5052i
-0.1199 - 0.2760i 0.0384 + 0.3955i
-0.1192 - 0.2810i 0.0297 + 0.2859i
-0.1186 - 0.2859i 0.0210 + 0.1762i
-0.1179 - 0.2908i 0.0124 + 0.0666i
-0.1172 - 0.2957i 0.0037 - 0.0430i
-0.1165 - 0.3006i -0.0050 - 0.1527i
-0.1159 - 0.3055i -0.0137 - 0.2623i
-0.1152 - 0.3104i -0.0224 - 0.3720i
-0.1145 - 0.3153i -0.0311 - 0.4816i
D =
1.0e+03 *
3.9211 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0029 - 0.0000i
R 复杂矩阵中的示例:
j=1
drop(Sigma_X[,,j])
Columns 1 through 4
346.0094+0.0000i 350.7470-3.0368i 355.4846-6.0736i 360.2222-9.1104i
350.7470+3.0368i 355.6162+0.0000i 360.4854-3.0368i 365.3546-6.0736i
355.4846+6.0736i 360.4854+3.0368i 365.4862+0.0000i 370.4870-3.0368i
360.2222+9.1104i 365.3546+6.0736i 370.4870+3.0368i 375.6194+0.0000i
364.9598+12.1472i 370.2238+9.1104i 375.4878+6.0736i 380.7518+3.0368i
369.6974+15.1839i 375.0930+12.1472i 380.4886+9.1104i 385.8842+6.0736i
374.4350+18.2207i 379.9622+15.1839i 385.4894+12.1472i 391.0166+9.1104i
379.1726+21.2575i 384.8314+18.2207i 390.4902+15.1839i 396.1490+12.1472i
383.9102+24.2943i 389.7006+21.2575i 395.4910+18.2207i 401.2814+15.1839i
388.6478+27.3311i 394.5698+24.2943i 400.4918+21.2575i 406.4138+18.2207i
Columns 5 through 7
364.9598-12.1472i 369.6974-15.1839i 374.4350-18.2207i
370.2238-9.1104i 375.0930-12.1472i 379.9622-15.1839i
375.4878-6.0736i 380.4886-9.1104i 385.4894-12.1472i
380.7518-3.0368i 385.8842-6.0736i 391.0166-9.1104i
386.0158+ 0.0000i 391.2798- 3.0368i 396.5438- 6.0736i
391.2798+ 3.0368i 396.6754+ 0.0000i 402.0710- 3.0368i
396.5438+ 6.0736i 402.0710+ 3.0368i 407.5982+ 0.0000i
401.8078+ 9.1104i 407.4666+ 6.0736i 413.1254+ 3.0368i
407.0718+12.1472i 412.8622+ 9.1104i 418.6526+ 6.0736i
412.3358+15.1839i 418.2578+12.1472i 424.1798+ 9.1104i
Columns 8 through 10
379.1726-21.2575i 383.9102-24.2943i 388.6478-27.3311i
384.8314-18.2207i 389.7006-21.2575i 394.5698-24.2943i
390.4902-15.1839i 395.4910-18.2207i 400.4918-21.2575i
396.1490-12.1472i 401.2814-15.1839i 406.4138-18.2207i
401.8078- 9.1104i 407.0718-12.1472i 412.3358-15.1839i
407.4666- 6.0736i 412.8622- 9.1104i 418.2578-12.1472i
413.1254- 3.0368i 418.6526- 6.0736i 424.1798- 9.1104i
418.7842+ 0.0000i 424.4430- 3.0368i 430.1018- 6.0736i
424.4430+ 3.0368i 430.2334+ 0.0000i 436.0237- 3.0368i
430.1018+ 6.0736i 436.0237+ 3.0368i 441.9457+ 0.0000i
如您所见,矩阵与之前在Matlab中计算的矩阵相同. 让我们计算特征值和特征向量.
As you can see, the matrix is the same as those computed before in Matlab. Let's compute eigenvalues and eigenvectors.
PP
[1,] -0.2967359+0.0000000i 0.50734838+0.00000000i
[2,] -0.3009476-0.0026119i 0.39737421-0.00152766i
[3,] -0.3051593-0.0052239i 0.28740004-0.00305533i
[4,] -0.3093709-0.0078358i 0.17742587-0.00458299i
[5,] -0.3135826-0.0104478i 0.06745170-0.00611065i
[6,] -0.3177943-0.0130597i -0.04252247-0.00763831i
[7,] -0.3220060-0.0156717i -0.15249664-0.00916598i
[8,] -0.3262177-0.0182836i -0.26247080-0.01069364i
[9,] -0.3304294-0.0208955i -0.37244497-0.01222130i
[10,] -0.3346411-0.0235075i -0.48241914-0.01374896i
DD
3921.066 0.000000
0.000 2.917833
如您所见,特征值与使用MATLAB计算的特征值相同,但特征向量不同.如何获得与MATLAB相同的特征向量?
As you can see, the eigenvalues are the same as those computed with MATLAB, but the eigenvectors are different. How can I get the same eigenvectors as those of MATLAB?
推荐答案
答案很简单,但由于向量很复杂而很难看清. 如果您在MATLAB中输入了两个矩阵,然后执行了
The answer is simple but difficult to see because the vectors are complex. If you enter both your matrices in MATLAB, and do this
P(:,1)./PP(:,1)
ans =
0.4064 + 0.9136i
0.4063 + 0.9136i
0.4063 + 0.9139i
0.4065 + 0.9138i
0.4064 + 0.9138i
0.4063 + 0.9138i
0.4063 + 0.9138i
0.4065 + 0.9137i
0.4064 + 0.9137i
0.4063 + 0.9137i
您会看到它们是线性相关的,而另一个是线性相关的.
you see that they are linearly dependent, same for the other one.
我担心,没有办法可以确保您在两个程序中获得相同的结果.通常,计算特征值和向量并不容易,实现中的细微差异也会导致您所看到的差异.
I am afraid, that there is no way that I know of, that guarantees you the same result within both programs. Computing eigenvalues and vectors in general is not easy, and slight differences in implementation result in the differences that you see.
要获得相同的结果,您可以尝试使用向量的第一个分量对向量进行归一化
To get the same result you could try normalizing the vectors with their first component, meaning
P(:,1)/P(1,1) and PP(:,1)/PP(1,1)
这为我提供了相同的向量,但模数差异很小,因为您为示例提供了不同数量的数字.
That gives me the same vectors, modulo small differences, as you provide a different number of digits for your examples.
另一项测试表明,除了将长度标准化为1之外,这确实是Matlab所做的.
Another test shows, that this is indeed what Matlab does, in addition to normalizing the length to 1. So
tmp=P(:,1)/P(1,1);
tmp/norm(tmp)
返回与您的Matlab示例相同的向量.
returns the same vector as your Matlab Example.
这篇关于R中的特征向量复数非对称矩阵不同于Matlab:如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!