Eigenvalues for matrices in a for loop

允我心安 提交于 2019-12-11 17:54:14

问题


I need to calculate eigenvalues of a series of matrices and then save them in a separate file. My data has 5 columns and 10,000 rows. I use the following functions:

        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)
    }

As an example I have used a data set with 10 rows and 5 columns. This gives me the following results:

$`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

I have three questions and I would really appreciate any help:

  1. I want to save the results in consecutive rows, such as:

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

    any thoughts?

  2. Also, I don't understand the eigenvalues in the output. They are not numbers. For example, one of them is 2.362320e+02+0.000000e+00i. My first though was that this is the sum of five determinants for a 5x5 matrix. However, "2.362320e+02+0.000000e+00i" seems to only have four numbers in it. Any thoughts? Doesn't eigen() function calculate the final values of eigenvalues?

  3. how can I save my outcome on an Excel file? I have used the following codes

However, the result I get from the current codes are:

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

回答1:


I think, you can easily get values by the following code:

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)}

}

and then use the answer of this question, to save R into a file



来源:https://stackoverflow.com/questions/52134994/eigenvalues-for-matrices-in-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!