Skip to content

Instantly share code, notes, and snippets.

@esromneb
Last active July 21, 2022 12:28
Show Gist options
  • Save esromneb/652fed46ae328b17e104 to your computer and use it in GitHub Desktop.
Save esromneb/652fed46ae328b17e104 to your computer and use it in GitHub Desktop.
matlab's rref function modified to operate in gf(2)
% This is a modified version of matlab's building rref which calculates
% row-reduced echelon form in gf(2). Useful for linear codes.
% Tolerance was removed because yolo, and because all values
% should only be 0 or 1. @benathon
function [A] = g2rref(A)
%G2RREF Reduced row echelon form in gf(2).
% R = RREF(A) produces the reduced row echelon form of A in gf(2).
%
% Class support for input A:
% float: with values 0 or 1
% Copyright 1984-2005 The MathWorks, Inc.
% $Revision: 5.9.4.3 $ $Date: 2006/01/18 21:58:54 $
[m,n] = size(A);
% Loop over the entire matrix.
i = 1;
j = 1;
while (i <= m) && (j <= n)
% Find value and index of largest element in the remainder of column j.
k = find(A(i:m,j),1) + i - 1;
% Swap i-th and k-th rows.
A([i k],j:n) = A([k i],j:n);
% Save the right hand side of the pivot row
aijn = A(i,j:n);
% Column we're looking at
col = A(1:m,j);
% Never Xor the pivot row against itself
col(i) = 0;
% This builds an matrix of bits to flip
flip = col*aijn;
% Xor the right hand side of the pivot row with all the other rows
A(1:m,j:n) = xor( A(1:m,j:n), flip );
i = i + 1;
j = j + 1;
end
@mozaffarinima
Copy link

Hi,
Thank you for providing this efficient well working code. I was dealing with this problem for a while and your code helped me a lot. However, I need the result to be in the form I have sent you the picture for and the result that I get using your code is not like that. can you help me with that? thanks
1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment