【问题标题】:Eigenvalues for matrices in a for loopfor循环中矩阵的特征值
【发布时间】:2018-09-02 07:47:13
【问题描述】:

我需要计算一系列矩阵的特征值,然后将它们保存在单独的文件中。我的数据有 5 列和 10,000 行。我使用以下功能:

        R<-NULL

    A <- setwd("c:/location of the file on this computer")
    for(i in 0:1){
      X<-read.table(file="Example.prn", skip=i*5, nrow=5)
      M <- as.matrix(X)
      E=eigen(M, only.values = TRUE)
      R<-rbind(R,E)}
      print(E)
    }

作为示例,我使用了一个包含 10 行和 5 列的数据集。这给了我以下结果:

$`values`
[1]  1.350000e+02+0.000e+00i -4.000000e+00+0.000e+00i  4.365884e-15+2.395e-15i  4.365884e-15-2.395e-15i
[5]  8.643810e-16+0.000e+00i

$vectors
NULL

$`values`
[1]  2.362320e+02+0.000000e+00i -4.960046e+01+1.258757e+01i -4.960046e+01-1.258757e+01i  9.689475e-01+0.000000e+00i
[5]  1.104994e-14+0.000000e+00i

$vectors
NULL

我有三个问题,非常感谢您的帮助:

  1. 我想将结果保存在连续的行中,例如:

    Eigenvalue(1) Eigenvalue(3) Eigenvalue(5) Eigenvalue(7) Eigenvalue(9)
    Eigenvalue(2) Eigenvalue(4) Eigenvalue(6) Eigenvalue(8) Eigenvalue(10)
    

    有什么想法吗?

  2. 另外,我不理解输出中的特征值。它们不是数字。例如,其中之一是 2.362320e+02+0.000000e+00i。我的第一个想法是,这是 5x5 矩阵的五个行列式的总和。但是,“2.362320e+02+0.000000e+00i”似乎只有四个数字。有什么想法吗? eigen()函数不计算特征值的最终值吗?

  3. 如何将结果保存在 Excel 文件中?我使用了以下代码

但是,我从当前代码中得到的结果是:

> class(R)
[1] "matrix"
> print(R)
  values    vectors   
E Complex,5 NULL
E Complex,5 NULL

【问题讨论】:

  • E[, 1] 中的错误:维数不正确
  • 你应该通过E$values得到正确的值?

标签: for-loop output eigenvalue


【解决方案1】:

我认为,您可以通过以下代码轻松获取值:

R<-NULL

A <- setwd("c:/location of the file on this computer")
for(i in 0:1){
  X<-read.table(file="Example.prn", skip=i*5, nrow=5)
  M <- as.matrix(X)
  E=eigen(M, only.values = TRUE)
  R<-rbind(R,E$values)}

}

然后使用this question的答案,将R保存到文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2019-04-27
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多