% Returns sparse diagonal matrix of size [n,n] or [n,d]
% 
% [I] = sparseDiag(v,n,d)
% v - vector of diagonal entries
% n - number of rows
% d - number of columns (optional)
% 
% Written by Jason Rennie, December 2003
% Last modified: Tue May 24 17:53:28 2005

function [I] = sparseDiag(v,n,d)
if (nargin < 2 | nargin > 3)
  error('Wrong number of arguments (%d)',nargin)
end
if (nargin == 2)
  d = n;
end
m = min(n,d);
if (length(v) ~= m)
  error('Vector of diagonal entries must have length min(n,d) (%d)',length(v));
end
I = sparse(1:m,1:m,v,n,d);
