# Getting square matrix of dimension matrixrank

I have matrix with dimensions {24,33} and matrix rank 23, it is symbolic. Is there a way to get matrix with dimension {23,23} by using built-in Mathematica functions? I would know how to get it simple..by taking one by one row which increases rank and than transpose it and do the same again. Is there a easier way?

• Could you post a smaller example? Lest say with a 5x3 matrix. Nov 3, 2021 at 15:58
• best I could think of..but with numbers. so I need to get mat1 from mat2. thanx mat1 = Table[RandomInteger, {ii, 1, 3}, {jj, 1, 3}]; mat2 = Append[mat1, mat1[]]; mat2 = Transpose[mat2]; mat2 = Join[mat2, {mat2[] + mat2[], mat2[] - mat2[]}]; mat2 = Transpose[mat2]; Nov 3, 2021 at 16:15
• What is the relation between the {24,33} and {23,23} matrix? Nov 3, 2021 at 16:52
• I fully concur with the @DanielHuber comment. You have to say how the resulting {23,23} matrix should be related to the initial {24,33} matrix. Nov 3, 2021 at 17:49
• long story, I got bigger matrix from homogenous system of linear equations, it is overdetermined, I need to calculate its determinant and it has to square matrix. Nov 3, 2021 at 17:49

This might work.

a = RandomChoice[Alphabet[], {5, 5}];

a = Insert[a, 2 a[], 4];
a = Insert[a, 3 a[], 6];

MatrixForm[a]


$$\begin{pmatrix} \text{f} & \text{s} & \text{j} & \text{v} & \text{p} \\ \text{j} & \text{v} & \text{k} & \text{m} & \text{b} \\ \text{m} & \text{v} & \text{i} & \text{h} & \text{n} \\ 2 \text{f} & 2 \text{s} & 2 \text{j} & 2 \text{v} & 2 \text{p} \\ \text{z} & \text{e} & \text{k} & \text{n} & \text{x} \\ 3 \text{j} & 3 \text{v} & 3 \text{k} & 3 \text{m} & 3 \text{b} \\ \text{i} & \text{n} & \text{l} & \text{d} & \text{o} \\ \end{pmatrix}$$

Dimensions[a]
MatrixRank[a]


{7,5}

5

b = Transpose@RowReduce@Transpose@a;
c = a[[Position[b, 1][[All, 1]]]];
MatrixForm[c]


$$\begin{pmatrix} \text{f} & \text{s} & \text{j} & \text{v} & \text{p} \\ \text{j} & \text{v} & \text{k} & \text{m} & \text{b} \\ \text{m} & \text{v} & \text{i} & \text{h} & \text{n} \\ \text{z} & \text{e} & \text{k} & \text{n} & \text{x} \\ \text{i} & \text{n} & \text{l} & \text{d} & \text{o} \\ \end{pmatrix}$$

• thank but no :( sorry I don't know how to insert new lines..try with this example: a = {{2 I A + 2 I B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 2, 2 G1, 0, 0, 0, 0, 2 I A + 2 I B}, {0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B, 0}, {0, 0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B - G1 - G2, 0, 0}, {0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B, 0, 0, 0}, {0, 2, 0, 0, 0, 0, 2 G2, 2 I A + 2 I B, 0, 0, 0, 0}}; Nov 3, 2021 at 17:41

OK, here's a solution. My assumption is you want the candidate smaller matrix to have the same elements in the same order as the larger one, but with extra rows and columns removed. This generates all possible solutions.

  a = {{2 I A + 2 I B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 2 G1, 0, 0, 0, 0, 2 I A + 2 I B},
{0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B, 0},
{0, 0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B - G1 - G2, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0, 2 I A + 2 I B, 0, 0, 0},
{0, 2, 0, 0, 0, 0, 2 G2, 2 I A + 2 I B, 0, 0, 0, 0}};


Get the rank

 mr=MatrixRank@a
(* 6 *)


Get the dimensions

 {rise, run} = Dimensions@a


Get all possible permutations of the indices

 rises = Subsets[Range[rise], {mr}];
runs = Subsets[Range[run], {mr}];


Get all permutations of matrices

 matrices = Flatten[Outer[a[[#1, #2]] &, rises, runs, 1], 1];


There are lots of them

 Length@matrices
(* 924 *)


Get just the ones that satisfy your requirement that they be full rank

 goodmatrices = Select[matrices, MatrixRank[#] == mr &];


Of which there are still quite a few

 Length@goodmatrices
(* 64 *)



This can all be combined into a neat function.

• thank you, it looks ok. my question was more if it can be done with rowreduce of some transformation already implemented in mathematica, something like Sumit suggested. I know how to do it ... Nov 5, 2021 at 13:30
• You were never clear on exactly what you wanted, that the start matrix would be transformed into. It's still an open question. Nov 5, 2021 at 18:46
• let's say to reduce matrix by eliminating dependent columns and rows, but by using mathematica builtin functions, like rowreduce or something else. this is the way, and there are other, but I wonder is there something simpler: ll = Table[MatrixRank[Take[mat, ii]], {ii, 1, Length[mat]}]; gde = Table[FirstPosition[ll, ii], {ii, 1, MatrixRank[mat]}] // Flatten; Rmat = mat[[gde]]; Rmat = Transpose[Rmat]; ll = Table[MatrixRank[Take[Rmat, ii]], {ii, 1, Length[Rmat]}]; gde = Table[FirstPosition[ll, ii], {ii, 1, MatrixRank[Rmat]}] // Flatten; Rmat = Rmat[[gde]]; Nov 9, 2021 at 13:16