I have a (n, m) matrix A which has rank r < minimum([n, m]). I would like to find a basis of r vectors spanning the column/row space. How can I do that?
Here's a how one could generate the data. Since the rank is 3 I would expect to be able to find a basis of 3 vectors.
using LinearAlgebra: svd, rank, Diagonal
function generate_rank_r_matrix(r; n=20, m=10)
U, S, V = svd(randn(n, m))
return U[:, 1:r] * Diagonal(S[1:r]) * V[:, 1:r]'
end
A = generate_rank_r_matrix(3)
rank(A)
Attempt
I know that the Q matrix of the qr decomposition essentially contains some basis for the column space but I am unsure about how to find the r vectors only.
Perhaps one could use the SVD?