# How to find the linearly independent columns (rows) of a matrix

**URL:** <https://discourse.julialang.org/t/how-to-find-the-linearly-independent-columns-rows-of-a-matrix/109772>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [February 6, 2024, 12:05am UTC](https://discourse.julialang.org/t/how-to-find-the-linearly-independent-columns-rows-of-a-matrix/109772 "2024-02-06T00:05:39Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 6, 2024, 6:49pm UTC](https://discourse.julialang.org/t/how-to-find-the-linearly-independent-columns-rows-of-a-matrix/109772/14 "2024-02-06T18:49:08Z")

</div>

> [@qua4tre](#):
>
> Wouldn’t A_PA∗PA_P already be permuted so that the first rank(A)rank(A)rank(A) columns of AAA are linearly independent?

Yes, I think you can use something like:

```julia
using LinearAlgebra
function independent_cols(A; rtol=size(A,2)*eps(eltype(A)))
    QR = qr(A, ColumnNorm())
    atol = rtol * abs(QR.R[1,1])
    # just use QR as alternative to rank(A) (which computes the SVD):
    rank = something(findfirst(i -> abs(QR.R[i,i]) <= atol, 1:size(A,2)), size(A,2)+1) - 1
    return QR.p[1:rank]
end

```

to get a list of the independent columns up to some tolerance.

PS. For the algorithm in the linked paper, note that the expression R\_1 = C\_1^T (C\_1 C\_1^T)^{-1} is a badly conditioned way to compute the pseudo-inverse of C\_1 — you would be generally better off using the QR factorization (of C^T or C\_1^T, and you already have the former from computing the independent rows of C so you might as well re-use it) or the SVD for this. In general, the formulations in that paper appear oriented towards exact arithmetic and seem like they would benefit from some re-formulation using SVD or pivoted QR.

PPS. Seems like `rank(QR)` should be implemented. I filed an issue: [rank(::QRPivoted) method? · Issue #53214 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53214)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-find-the-linearly-independent-columns-rows-of-a-matrix/109772)._
