Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

38 líneas
995B

  1. # Josua Kugler, Christian Merten
  2. n_diag <- function(mat, n) {
  3. stopifnot("Matrix not quadratic" = nrow(mat) == ncol(mat))
  4. stopifnot("Matrix does not have such a (sub)diagonal" = abs(n) < nrow(mat))
  5. dg <- c()
  6. for (i in (1:(nrow(mat)-abs(n)))) {
  7. elem <- if (n > 0) {
  8. mat[i, i+n]
  9. } else {
  10. mat[i+abs(n), i]
  11. }
  12. dg <- c(dg, elem)
  13. }
  14. return(dg)
  15. }
  16. `n_diag<-` <- function(x, value, n) {
  17. stopifnot("Matrix not quadratic" = nrow(mat) == ncol(mat))
  18. stopifnot("Matrix does not have such a (sub)diagonal" = abs(n) < nrow(mat))
  19. stopifnot("Subdiagonal length and replacement do not match" = length(value) == nrow(mat) - abs(n))
  20. for (i in (1:(nrow(mat)-abs(n)))) {
  21. elem <- if (n > 0) {
  22. mat[i, i+n] <- value[i]
  23. } else {
  24. mat[i+abs(n), i] <- value[i]
  25. }
  26. }
  27. return(mat)
  28. }
  29. mat <- matrix(1:16, nrow=4)
  30. print(mat)
  31. print(n_diag(mat, 2))
  32. n_diag(mat, -3) <- 1
  33. print(mat)