You should not compute x_B as x_B = inv(B)*b, but as the solution of a linear system: x_B = B\b, and check that the rank of B is equal to m