I'm a bit confused. A matrix A has 8 eigenvalues, some of them are complex, some are real. I want to copy only the eigenvectors that correspond to the real eigenvalues as columns into a Matrix M, otherwise the column should be filled with 0s. I use the code:
M <- matrix(,ncol=8,nrow=8)
for(i in 1:8) {
M[,i] <- ifelse(Im(eigen(A)$val[i]) == 0, eigen(A)$vec[,i], 0)
}
but the result is as if I executed this one:
M <- matrix(,ncol=8,nrow=8)
for(i in 1:8) {
M[,i] <- ifelse(Im(eigen(A)$val[i]) == 0, eigen(A)$vec[**1**,i], 0)
}
(that BTW in deed generates exactly the same output as the code above). Where is my misconception?