[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

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Rui Barradas
icitly.     >>> A <- matrix(1:9,ncol=3)     >>> x <- c(0,1,0)     >>> M <- matrix(ncol=3,nrow=3)     >>> for(j in 1:3){     >>>     for(i in 1:3){     >>>       ifelse(x[i]==1, M[j,i]<-0, M[j,i]<-A[j,i])     >>>     }     &

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Bert Gunter
> >> > > M[,i] <-0 > >> > > } > >> > >} > >> > > } > >> > > M > >> > > > >> > > The outcome you want is to set all of the middle column values to > >> > > zer

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Bert Gunter
he middle column values to zero. >> > > So I used x as a logical in an if test and when true everything in that >> > > column is set to zero. >> > > >> > > Your approach also works but you must go through each element explicitly. >> > >

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Ivan Calandra
=1, M[j,i]<-0, M[j,i]<-A[j,i]) } } M Tim -Original Message- From: R-help On Behalf Of Uwe Freier Sent: Sunday, April 24, 2022 11:06 AM To: r-help@r-project.org Subject: [R] Confusing fori or ifelse result in matrix manipulation [External Email] Hello, sorry for the newbie qu

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Eric Berger
used x as a logical in an if test and when true everything in > that column is set to zero. > > > > > > Your approach also works but you must go through each element > explicitly. > > > A <- matrix(1:9,ncol=3) > > > x <- c(0,1,0) > > > M <- matrix

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Bert Gunter
:9,ncol=3) > > x <- c(0,1,0) > > M <- matrix(ncol=3,nrow=3) > > for(j in 1:3){ > >for(i in 1:3){ > > ifelse(x[i]==1, M[j,i]<-0, M[j,i]<-A[j,i]) > >} > > } > > M > > > > > > > > Tim > > > >

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Ivan Calandra
11:06 AM To: r-help@r-project.org Subject: [R] Confusing fori or ifelse result in matrix manipulation [External Email] 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) {

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Ebert,Timothy Aaron
-help On Behalf Of Uwe Freier Sent: Sunday, April 24, 2022 11:06 AM To: r-help@r-project.org Subject: [R] Confusing fori or ifelse result in matrix manipulation [External Email] Hello, sorry for the newbie question but I can't find out where I'm wrong. A <- matrix(1:9,ncol=3) x <

Re: [R] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Ivan Krylov
В Sun, 24 Apr 2022 17:05:55 +0200 Uwe Freier пишет: > ifelse(x[i] == 0, A[,i], 0) Hint: what does ifelse return instead of a vector of length nrow(A)? Since you're checking conditions of length 1, you can safely use `if (x[i] == 0) A[,i] else 0` here, or you can transform the `x` vector into a

[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