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
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])
>>> }
&
> >> > > M[,i] <-0
> >> > > }
> >> > >}
> >> > > }
> >> > > M
> >> > >
> >> > > The outcome you want is to set all of the middle column values to
> >> > > zer
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.
>> > >
=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
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
: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
> >
> >
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) {
-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 <
В 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
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
11 matches
Mail list logo