idamnjanovic@60: function Y = spdiag(V,K) idamnjanovic@60: %SPDIAG Sparse diagonal matrices. idamnjanovic@60: % SPDIAG(V,K) when V is a vector with N components is a sparse square idamnjanovic@60: % matrix of order N+ABS(K) with the elements of V on the K-th diagonal. idamnjanovic@60: % K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 idamnjanovic@60: % is below the main diagonal. idamnjanovic@60: % idamnjanovic@60: % SPDIAG(V) is the same as SPDIAG(V,0) and puts V on the main diagonal. idamnjanovic@60: % idamnjanovic@60: % See also DIAG, SPDIAGS. idamnjanovic@60: idamnjanovic@60: idamnjanovic@60: % Ron Rubinstein idamnjanovic@60: % Computer Science Department idamnjanovic@60: % Technion, Haifa 32000 Israel idamnjanovic@60: % ronrubin@cs idamnjanovic@60: % idamnjanovic@60: % June 2008 idamnjanovic@60: idamnjanovic@60: idamnjanovic@60: if (nargin<2) idamnjanovic@60: K = 0; idamnjanovic@60: end idamnjanovic@60: idamnjanovic@60: n = length(V) + abs(K); idamnjanovic@60: idamnjanovic@60: if (K>0) idamnjanovic@60: i = 1:length(V); idamnjanovic@60: j = K+1:n; idamnjanovic@60: elseif (K<0) idamnjanovic@60: i = -K+1:n; idamnjanovic@60: j = 1:length(V); idamnjanovic@60: else idamnjanovic@60: i = 1:n; idamnjanovic@60: j = 1:n; idamnjanovic@60: end idamnjanovic@60: idamnjanovic@60: Y = sparse(i,j,V(:),n,n);