% Brief summary of this function.
% Detailed explanation of this function.

function x = tomp(y, D, niter)
   nrows = size(D, 1);
   ncols = size(D, 2);
   x = zeros(ncols, 1);
   supp = [];
   res = zeros(max(size(y)),1);
   res(:,1) = y(:,1);
   i = 0;
   K = min(nrows, ncols);
   while (size(supp, 1) < K && i < niter)
      [~, j ] = max(abs(D'*res));
      supp = [ supp j];
      x(supp, :) = pinv(D(:,supp))*y;
      if (size(supp, 2) == 1)
          res = y - full(D(:, supp))*x(supp,:);
      else
          res = y - D(:, supp)*x(supp, :);       
      end
      i = i + 1;
   end
end