Translated setdiag()
This commit is contained in:
parent
f2f59ba7f8
commit
76d31e593a
1 changed files with 14 additions and 16 deletions
30
R/setdiag.R
30
R/setdiag.R
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue