[R] how ot replace the diagonal of a matrix
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Oct 3 23:22:30 CEST 2006
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
More information about the R-help
mailing list