annotate examples/private/spdiag.m @ 5:f44689e95ea4

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