annotate toolboxes/FullBNT-1.0.7/KPMtools/setdiag.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function M = setdiag(M, v)
Daniel@0 2 % SETDIAG Set the diagonal of a matrix to a specified scalar/vector.
Daniel@0 3 % M = set_diag(M, v)
Daniel@0 4
Daniel@0 5 n = length(M);
Daniel@0 6 if length(v)==1
Daniel@0 7 v = repmat(v, 1, n);
Daniel@0 8 end
Daniel@0 9
Daniel@0 10 % e.g., for 3x3 matrix, elements are numbered
Daniel@0 11 % 1 4 7
Daniel@0 12 % 2 5 8
Daniel@0 13 % 3 6 9
Daniel@0 14 % so diagnoal = [1 5 9]
Daniel@0 15
Daniel@0 16
Daniel@0 17 J = 1:n+1:n^2;
Daniel@0 18 M(J) = v;
Daniel@0 19
Daniel@0 20 %M = triu(M,1) + tril(M,-1) + diag(v);
Daniel@0 21