[R] Confusing fori or ifelse result in matrix manipulation

Uwe Freier uwe @end|ng |rom |re|er@de
Tue Apr 26 20:10:58 CEST 2022


Hello,

many thanks for your replies. The code I've posted was simplified and 
depicted my problem basically. Of course my intention was not to add 
0-columns to a matrix, the problem resulted from my approach to add only 
eigenvectors to a matrix if the corresponding eigenvalue is element of 
real numbers, otherwise 0. So now I've learned that following code does 
it:

A is the matrix with the eigenvalues and -vectors of my interest
M <- matrix(ncol=8,nrow=8)
> for(i in 1:8) {
      M[,i] <- if(Im(eigen(A)$val[i]) == 0) eigen(A)$vec[,i] else 0
}

Many thanks again!

Uwe



More information about the R-help mailing list