Dear useRs, Trying to replace the diagonal of a matrix is not working for me. I want a matrix with .6 on the diag and .4 elsewhere. The following code looks like it should work--when I lookk at mps and idx they look how I want them too--but it only replaces the first element, not each element on the diagonal. mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) idx <- diag(3) mps idx mps[idx] <- rep(.6,3) I also tried something along the lines of diag(mps=.6, ...) but it didn't know what mps was. Thanks, Roger
On 10/3/2006 4:59 PM, roger bos wrote:> Dear useRs, > > Trying to replace the diagonal of a matrix is not working for me. I > want a matrix with .6 on the diag and .4 elsewhere. The following > code looks like it should work--when I lookk at mps and idx they look > how I want them too--but it only replaces the first element, not each > element on the diagonal. > > mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) > idx <- diag(3) > mps > idx > mps[idx] <- rep(.6,3) > > I also tried something along the lines of diag(mps=.6, ...) but it > didn't know what mps was.Matrix indexing can use a two column matrix, giving row and column numbers. So you could get what you want by mps[cbind(1:n,1:n)] <- 0.6
You are indexing with numeric 0's and 1's, which will refer to only the matrix element 1,1 (multiple times), cf: > matrix(1:9,3)[diag(3)] [1] 1 1 1 > Try one of these: > idx <- diag(3) > 0 > idx <- which(diag(3)>0) > idx <- cbind(seq(len=n), seq(len=n)) (For very large matrices, the third will be more efficient, I believe.) -- Tony Plate roger bos wrote:> Dear useRs, > > Trying to replace the diagonal of a matrix is not working for me. I > want a matrix with .6 on the diag and .4 elsewhere. The following > code looks like it should work--when I lookk at mps and idx they look > how I want them too--but it only replaces the first element, not each > element on the diagonal. > > mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) > idx <- diag(3) > mps > idx > mps[idx] <- rep(.6,3) > > I also tried something along the lines of diag(mps=.6, ...) but it > didn't know what mps was. > > Thanks, > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
try:> mps <- matrix(rep(.4, 3*3), nrow=3, byrow=TRUE) > idx <- diag(3) > mps[,1] [,2] [,3] [1,] 0.4 0.4 0.4 [2,] 0.4 0.4 0.4 [3,] 0.4 0.4 0.4> idx[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1> mps[idx == 1] <- rep(.6,3) > > mps[,1] [,2] [,3] [1,] 0.6 0.4 0.4 [2,] 0.4 0.6 0.4 [3,] 0.4 0.4 0.6>On 10/3/06, roger bos <roger.bos at gmail.com> wrote:> Dear useRs, > > Trying to replace the diagonal of a matrix is not working for me. I > want a matrix with .6 on the diag and .4 elsewhere. The following > code looks like it should work--when I lookk at mps and idx they look > how I want them too--but it only replaces the first element, not each > element on the diagonal. > > mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) > idx <- diag(3) > mps > idx > mps[idx] <- rep(.6,3) > > I also tried something along the lines of diag(mps=.6, ...) but it > didn't know what mps was. > > Thanks, > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On 10/3/06, roger bos <roger.bos at gmail.com> wrote:> Dear useRs, > > Trying to replace the diagonal of a matrix is not working for me. I > want a matrix with .6 on the diag and .4 elsewhere. The following > code looks like it should work--when I lookk at mps and idx they look > how I want them too--but it only replaces the first element, not each > element on the diagonal. > > mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) > idx <- diag(3) > mps > idx > mps[idx] <- rep(.6,3) > > I also tried something along the lines of diag(mps=.6, ...) but it > didn't know what mps was.You got close. diag(mps) <- .6 should give you what you want. ?diag (which you should have read) explains this usage quite clearly. -Deepayan> > Thanks, > > Roger