To rank an approximation in MATLAB, we can use the singular value decomposition (SVD) method along with the least squares method.
First, we can generate a matrix with some random values. For example:
main.m19 chars2 lines
Next, we can calculate the SVD of this matrix:
main.m20 chars2 lines
The singular values are stored in the diagonal matrix S. We can plot these values to determine the rank of the matrix:
main.m20 chars2 lines
The rank of the matrix can be determined by finding the number of significant singular values. We can determine the threshold by inspecting the plot and visually determining where the significant values end:
main.m84 chars3 lines
Finally, we can calculate the best rank-k approximation of the matrix using the least squares method:
main.m69 chars3 lines
This approximation will have rank k, and will be the best approximation in terms of the least squares method.
gistlibby LogSnag