Hi, Duncan,

thx for your valuable hint re the two assigment functions! That might be the reason. Nevertheless, I couldn't figure out how they work together here. I simply executed the relevant lines of code which I found in `diag<-`body after assigning the matrix to x (which, of course, cannot yield the same as diag(A[-1,]) <- 7 in the light of your explanation):

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

x <- A[-1,]
value <- 7


# From `diag<-`'s body:

dx <- dim(x)
len.i <- min(dx)
len.v <- length(value)
if (len.i) {
  i <- seq_len(len.i)
  x[cbind(i, i)] <- value
  }
x



I fiddled a bit around with expressions, but to no avail.
Could you (or anybody else) maybe still shed some more light on this?
Thx a lot in advance!

 Best  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 215
gerrit.eich...@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 15:31 schrieb Duncan Murdoch:
On 2024-12-04 7:38 a.m., Gerrit Eichner 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


Could you show us the log of what you did that generated errors?  The statement `diag(A[-1, ]) <- 2` is pretty complex; it involves two assignment functions (both `diag<-` and `[<-`), so you might have tried to execute the wrong thing.

Duncan Murdoch


______________________________________________
R-help@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.

Reply via email to