You can find the rank of the Boolean Matrix by finding rank in the Galois Field of 2 (as you are doing in your Matlab code), which is essentially mod 2 arithmetic.
The code below finds the rank of the Boolean Matrix using the same idea by using Gauss Elimination with partial pivoting.
#include <iostream>
#include <vector>
using namespace std;
class BooleanMatrix{
vector< vector<bool> > mat; //boolean matrix
int n, m; //size of matrix nxm
int rank; //rank of the matrix
public:
/*Constructor
* Required Parameters:
* M ==> boolean matrix
* n ==> number of rows
* m ==> number of columns
*/
template <size_t size_m>
BooleanMatrix(bool M[][size_m], int n, int m){
this -> n = n;
this -> m = m;
for (int i = 0; i < n; i++){
vector<bool> row(m);
for (int j = 0; j < m; j++) row[j] = M[i][j];
mat.push_back(row);
}
gaussElimination();
}
/* Does Gauss Elimination with partial pivoting on the matrix */
void gaussElimination(){
rank = n;
for (int i = 0; i < n; i++){
if (!mat[i][i]){
int j;
for (j = i+1; j < n && !mat[j][i]; j++);
if (j == n){
rank--;
continue;
}
else
for (int k = i; k < m; k++){
bool t = mat[i][k];
mat[i][k] = mat[j][k];
mat[j][k] = t;
}
}
for (int j = i+1; j < n; j++){
if (mat[j][i]){
for (int k = i; k < m; k++)
mat[j][k] = mat[j][k] - mat[i][k];
}
}
}
}
/* Get the row rank of the boolean matrix
* If you require the rank of the matrix, make sure that n > m.
* i.e. if n < m, call the constructor over the transpose.
*/
int getRank(){
return rank;
}
};
int main(){
bool M1[3][3] = { {1, 0, 1},
{0, 1, 1},
{1, 1, 0} };
BooleanMatrix booleanMatrix1(M1, 3, 3);
cout << booleanMatrix1.getRank() << endl;
bool M2[4][4] = { {1,1,1,0},
{0,1,1,0},
{0,1,0,0},
{1,1,1,1} };
BooleanMatrix booleanMatrix2(M2, 4, 4);
cout << booleanMatrix2.getRank() << endl;
}
This gives result as expected for both the case. The algorithm should work well for all practical purposes. Trivial improvements & application specific changes could be made to suit as per your requirements.
I haven't tested it thoroughly though. If anybody finds any bug, please edit the answer to correct it.
Hope this helps.