wolffd@0: function M = setdiag(M, v) wolffd@0: % SETDIAG Set the diagonal of a matrix to a specified scalar/vector. wolffd@0: % M = set_diag(M, v) wolffd@0: wolffd@0: n = length(M); wolffd@0: if length(v)==1 wolffd@0: v = repmat(v, 1, n); wolffd@0: end wolffd@0: wolffd@0: % e.g., for 3x3 matrix, elements are numbered wolffd@0: % 1 4 7 wolffd@0: % 2 5 8 wolffd@0: % 3 6 9 wolffd@0: % so diagnoal = [1 5 9] wolffd@0: wolffd@0: wolffd@0: J = 1:n+1:n^2; wolffd@0: M(J) = v; wolffd@0: wolffd@0: %M = triu(M,1) + tril(M,-1) + diag(v); wolffd@0: