[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Dec 5 14:29:22 CET 2024


Gerrit:
Oh, sorry. I was obviously unclear. What I meant by "equivalent" was
"this is how the subset of indices in A whose corresponding values are
changed is selected."  So the example was meant to show this, not as a
literal representation of the code.

This might be a clearer explanation, but feel free to ignore and
certainly no need to reply.

A <- matrix(0, nrow =5, ncol = 5)

Here is what  "diag(A[-1,] <- 1" does (semantically):
B <- A[-1,]
diag(B) <- 1
A[-1,] <- B  ## etc. for all your examples

This makes semantic sense to me, i.e. what you want "diag(A[-1,] <- 1"
to mean. This puzzled me, but maybe you understood this already and
just wanted to understand how the code does this. If so, my apologies
for the spam.

Cheers,
Bert

On Thu, Dec 5, 2024 at 3:03 AM Gerrit Eichner
<gerrit.eichner using math.uni-giessen.de> wrote:
>
> Hi, Bert,
>
> thx, but I don't think so, because your example fails, if you use
> different entries for matrix A than your vector 1:25. (Try A filled with
> only zeroes, e.g.)
>
> I guess, Duncan Murdochs hint is crucial. I'll comment on his asap.
>
>   Best regards  --  Gerrit
>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 215
> gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> https://www.uni-giessen.de/math/eichner
> ---------------------------------------------------------------------
>
> Am 04.12.2024 um 14:38 schrieb Bert Gunter:
> > matrices are vectors with a "dim" attribute.
> > So what I think is happening is:
> >
> >> A <- matrix(1:25, nrow = 5, ncol = 5)
> >> diag(A[-1,]) <- 0
> >> A
> >       [,1] [,2] [,3] [,4] [,5]
> > [1,]    1    6   11   16   21
> > [2,]    0    7   12   17   22
> > [3,]    3    0   13   18   23
> > [4,]    4    9    0   19   24
> > [5,]    5   10   15    0   25
> >>
> >> ## is equivqalent to:
> >>
> >> A <- matrix(1:25, nrow = 5, ncol = 5)
> >> wh <- c(diag(A[-1,]))   # A's vector indices of diag(A[-1,])
> >> A[wh] <- 0
> >> A
> >       [,1] [,2] [,3] [,4] [,5]
> > [1,]    1    6   11   16   21
> > [2,]    0    7   12   17   22
> > [3,]    3    0   13   18   23
> > [4,]    4    9    0   19   24
> > [5,]    5   10   15    0   25
> >
> > I didn't check, but I assume your other examples would work similarly.
> > Please repost if I am wrong.
> >
> > Cheers,
> > Bert
> >
> > On Wed, Dec 4, 2024 at 4:39 AM Gerrit Eichner
> > <gerrit.eichner using math.uni-giessen.de> wrote:
> >>
> >> Dear list,
> >>
> >> is anyone aware of the following behavious of diag when used to replace
> >> diagonals (plural!) of a matrix?
> >>
> >> Small example: The following is documented and clearly to be expected:
> >>
> >> A <- matrix(0, nrow = 5, ncol = 5)
> >> diag(A) <- 1; A
> >>
> >>
> >> BUT, what about the following? When executing the code of `diag<-` line
> >> by line, it throws errors. So why does it work?
> >>
> >> diag(A[-1, ]) <- 2; A
> >>
> >> diag(A[-5, -1]) <- 3; A
> >>
> >> diag(A[-5, -(1:2)]) <- 4; A
> >>
> >>
> >> Any ideas?
> >>
> >>    TIA and best regards  --  Gerrit
> >>
> >> ---------------------------------------------------------------------
> >> Dr. Gerrit Eichner                   Mathematical Institute, Room 215
> >> gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
> >> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> >> https://www.uni-giessen.de/math/eichner
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list