Search code examples
matrixjulialinear-algebranumerical-methods

Create random matrix with orthonormal columns?


In Julia, is there any function or way to generate a random matrix with orthonormal columns of arbitrary dimensions (not necessarily square, could be rectangular/tall matrix)?


Solution

  • Since the columns of Q in a QR decomposition are orthonormal, I think you could just perform a QR decomposition on a random matrix.

    julia> using LinearAlgebra
    
    julia> qr(rand(5)).Q
    5×5 LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}, Matrix{Float64}}:
     -0.418858  -0.339672   -0.113064   -0.664132   -0.505298
     -0.339672   0.918683   -0.0270673  -0.158992   -0.120967
     -0.113064  -0.0270673   0.99099    -0.0529223  -0.0402654
     -0.664132  -0.158992   -0.0529223   0.689136   -0.236517
     -0.505298  -0.120967   -0.0402654  -0.236517    0.820048
    

    If you don't need all 5 columns, you can just take the first k that you need.