function Q = spdiagmat(A,n)
  % ========================
  % https://guiard.org/pages/octave/spdiagmat.html
  % =========================
  %
  % Returns a sparse matrix of size [rows(A)*n columns(A)*n] with diag = A
  % Version 1.01
  %


  c = columns(A);
  r = rows(A);



  Q = sparse(n*r,c);

  Q(1:r,1:c) = A;

  Q = vec(Q);

  Q = [Q; sparse(r,1)];

  Q = repmat(Q,n,1);

  Q(end-r*n+1:end) = [];

  Q = reshape(Q,r*n,c*n);


endfunction






