-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsptoeplitz.m
More file actions
40 lines (35 loc) · 1.26 KB
/
sptoeplitz.m
File metadata and controls
40 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function T = sptoeplitz(col,row)
% SPTOEPLITZ Sparse Toeplitz matrix.
% SPTOEPLITZ(C,R) produces a sparse nonsymmetric Toeplitz matrix having
% C as its first column and R as its first row. Neither C nor R needs to
% be sparse. No full-size dense matrices are formed.
%
% SPTOEPLITZ(R) is a sparse symmetric/Hermitian Toeplitz matrix.
%
% Examples:
% sptoeplitz( real( (1i).^(0:8) ) ) % 9x9, 41 nonzeros
% sptoeplitz( [-2 1 zeros(1,9998)] ); % classic 2nd difference
%
% See also TOEPLITZ, SPDIAGS.
% Copyright (c) 2006 by Tobin Driscoll (tobin.driscoll@gmail.com).
% First version, 11 December 2006.
% This part is borrowed from built-in Toeplitz.
if nargin < 2 % symmetric case
col(1) = conj(col(1)); row = col; col = conj(col);
else
if col(1)~=row(1)
warning('MATLAB:sptoeplitz:DiagonalConflict',['First element of ' ...
'input column does not match first element of input row. ' ...
'\n Column wins diagonal conflict.'])
end
end
% Size of result.
m = length(col(:)); n = length(row(:));
% Locate the nonzero diagonals.
[ic,jc,sc] = find(col(:));
row(1) = 0; % not used
[ir,jr,sr] = find(row(:));
% Use spdiags for construction.
d = [ ir-1; 1-ic ];
B = repmat( [ sr; sc ].', min(m,n),1 );
T = spdiags( B,d,m,n );