%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% function [Hfex] = nonsinglastnk(H);
%% This routine rearranges a parity matrix so that last n-k columns are 
%% linearly independet.  This helps with certain matlab ldpc objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of input variables
%% H      : parity-check matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of output variable
%% Hfix     : reindexes H columns so the last n-k are nonsingular
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EE379A, 02/24, J. Cioffi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Hfix] = nonsinglastnk(H);
[row col] = size(H);
%% eliminate columns from right side backwards
Hfix=H;
for i = 1:row
    while rank(gf(Hfix(:,end-row+1:end))) < row
        Hfix = [Hfix(:,end-i), Hfix(:,1:end-i-1), Hfix(:,end-i+1:end)];
    end
end
end

