annotate Problems/private/spdiag.m @ 61:42fcbcfca132

(none)
author idamnjanovic
date Tue, 15 Mar 2011 12:21:31 +0000
parents 207a6ae9a76f
children
rev   line source
idamnjanovic@10 1 function Y = spdiag(V,K)
idamnjanovic@10 2 %SPDIAG Sparse diagonal matrices.
idamnjanovic@10 3 % SPDIAG(V,K) when V is a vector with N components is a sparse square
idamnjanovic@10 4 % matrix of order N+ABS(K) with the elements of V on the K-th diagonal.
idamnjanovic@10 5 % K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0
idamnjanovic@10 6 % is below the main diagonal.
idamnjanovic@10 7 %
idamnjanovic@10 8 % SPDIAG(V) is the same as SPDIAG(V,0) and puts V on the main diagonal.
idamnjanovic@10 9 %
idamnjanovic@10 10 % See also DIAG, SPDIAGS.
idamnjanovic@10 11
idamnjanovic@10 12
idamnjanovic@10 13 % Ron Rubinstein
idamnjanovic@10 14 % Computer Science Department
idamnjanovic@10 15 % Technion, Haifa 32000 Israel
idamnjanovic@10 16 % ronrubin@cs
idamnjanovic@10 17 %
idamnjanovic@10 18 % June 2008
idamnjanovic@10 19
idamnjanovic@10 20
idamnjanovic@10 21 if (nargin<2)
idamnjanovic@10 22 K = 0;
idamnjanovic@10 23 end
idamnjanovic@10 24
idamnjanovic@10 25 n = length(V) + abs(K);
idamnjanovic@10 26
idamnjanovic@10 27 if (K>0)
idamnjanovic@10 28 i = 1:length(V);
idamnjanovic@10 29 j = K+1:n;
idamnjanovic@10 30 elseif (K<0)
idamnjanovic@10 31 i = -K+1:n;
idamnjanovic@10 32 j = 1:length(V);
idamnjanovic@10 33 else
idamnjanovic@10 34 i = 1:n;
idamnjanovic@10 35 j = 1:n;
idamnjanovic@10 36 end
idamnjanovic@10 37
idamnjanovic@10 38 Y = sparse(i,j,V(:),n,n);