[R] Confusing fori or ifelse result in matrix manipulation

2022-04-26 Thread Uwe Freier
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 rea

[R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Uwe Freier
Hello, sorry for the newbie question but I can't find out where I'm wrong. A <- matrix(1:9,ncol=3) x <- c(0,1,0) M <- matrix(ncol=3,nrow=3) for(i in 1:3) { M[,i] <- ifelse(x[i] == 0, A[,i], 0) } expected: M [,1] [,2] [,3] [1,]107 [2,]208 [3,]309 bu