Gerrit Eichner
2024-Dec-04 12:38 UTC
[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?
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 at 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
Bert Gunter
2024-Dec-04 13:38 UTC
[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?
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 at 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 at 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 at 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.
Duncan Murdoch
2024-Dec-04 14:31 UTC
[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?
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
Rui Barradas
2024-Dec-05 11:42 UTC
[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?
?s 12:38 de 04/12/2024, Gerrit Eichner escreveu:> 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 at 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 at 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.Hello, I cannot reproduce any error, everything works as expected. sessionInfo at the end. A <- matrix(0, nrow = 5, ncol = 5) diag(A) <- 1; A # [,1] [,2] [,3] [,4] [,5] # [1,] 1 0 0 0 0 # [2,] 0 1 0 0 0 # [3,] 0 0 1 0 0 # [4,] 0 0 0 1 0 # [5,] 0 0 0 0 1 diag(A[-1, ]) <- 2; A # [,1] [,2] [,3] [,4] [,5] # [1,] 1 0 0 0 0 # [2,] 2 1 0 0 0 # [3,] 0 2 1 0 0 # [4,] 0 0 2 1 0 # [5,] 0 0 0 2 1 diag(A[-5, -1]) <- 3; A # [,1] [,2] [,3] [,4] [,5] # [1,] 1 3 0 0 0 # [2,] 2 1 3 0 0 # [3,] 0 2 1 3 0 # [4,] 0 0 2 1 3 # [5,] 0 0 0 2 1 diag(A[-5, -(1:2)]) <- 4; A # [,1] [,2] [,3] [,4] [,5] # [1,] 1 3 4 0 0 # [2,] 2 1 3 4 0 # [3,] 0 2 1 3 4 # [4,] 0 0 2 1 3 # [5,] 0 0 0 2 1 sessionInfo() # R version 4.4.2 (2024-10-31 ucrt) # Platform: x86_64-w64-mingw32/x64 # Running under: Windows 11 x64 (build 22631) # # Matrix products: default # # # locale: # [1] LC_COLLATE=Portuguese_Portugal.utf8 LC_CTYPE=Portuguese_Portugal.utf8 # [3] LC_MONETARY=Portuguese_Portugal.utf8 LC_NUMERIC=C # [5] LC_TIME=Portuguese_Portugal.utf8 # # time zone: Europe/Lisbon # tzcode source: internal # # attached base packages: # [1] stats graphics grDevices utils datasets methods base # # loaded via a namespace (and not attached): # [1] compiler_4.4.2 # Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com