function Q = diagadd(Q,v) % DIAGADD Computes the sum of a matrix and a diagonal scalar entry. % % [Q] = DIAGADD(X,v) % % This function adds scalar quantities diagonally to a square matrix. It is % also equivalent to Q = X + v*eye(length(X)), where v is the scalar to be % added onto the diagonal matrix X, but in a much more efficient manner. % % Inputs: % X = Any square matrix % v = Scalar variable % % Output: % Q = Result of summation % % % (C) Gaussian Process Toolbox 1.0 % (C) Copyright 2005-2007, Keith Neo % http://www.hamilton.ie [N1,N2]=size(Q); if N1==N2 for i=1:N1 Q(i,i)=Q(i,i)+v; end else disp('Matrix is not a square matrix'); clear Q; end