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