Translated setdiag()

This commit is contained in:
Waldir Leoncio 2022-12-22 13:39:48 +01:00
parent f2f59ba7f8
commit 76d31e593a

View file

@ -1,21 +1,19 @@
setdiag <- function(M, v) {
stop("needs translation")
# function M = setdiag(M, v)
# % SETDIAG Set the diagonal of a matrix to a specified scalar/vector.
# % M = set_diag(M, v)
# SETDIAG Set the diagonal of a matrix to a specified scalar/vector.
# M <- set_diag(M, v)
# n = length(M);
# if length(v)==1
# v = repmat(v, 1, n);
# end
n <- length(M)
if (length(v) == 1) {
v <- repmat(v, c(1, n))
}
# % e.g., for 3x3 matrix, elements are numbered
# % 1 4 7
# % 2 5 8
# % 3 6 9
# % so diagnoal = [1 5 9]
# e.g., for 3x3 matrix, elements are numbered
# 1 4 7
# 2 5 8
# 3 6 9
# so diagnoal = [1 5 9]
# J = 1:n+1:n^2;
# M(J) = v;
J <- seq(1, n ^ 2, n + 1)
M[J] <- v
return(M)
}