[英]MATLAB: Find largest singular values of a matrix and corresponding columns
I have matrix A and matrices [U,S,V], such that [U, S, V] = svd(A). 我有矩阵A和矩阵[U,S,V],因此[U,S,V] = svd(A)。
How could I amend my script in matlab to get the 10 columns of U that correspond to the 10 largest singular values of A (ie the largest values in S)? 我如何在matlab中修改脚本以获得与A的10个最大奇异值(即S中的最大值)相对应的U的10列?
Thanks 谢谢
If you recall the definition of the svd
, it is essentially solving an eigenvalue problem such that: 如果您回想svd
的定义,它实际上是在解决一个特征值问题,例如:
Av = su
v
is a right-eigenvector from the matrix V
and u
is a left-eigenvector from the matrix U
. v
是来自矩阵V
的右特征向量,而u
是来自矩阵U
的左特征向量。 s
is a singular value from the matrix S
. s
是矩阵S
的奇异值。 You know that S
is a diagonal matrix where these are the singular values sorted in descending order. 您知道S
是一个对角矩阵,其中的奇异值是按降序排列的。 As such, if we took the first column of v
and the first column of u
, as well as the first singular value of s
(top-left corner), if we did the above computation, we should get both outputs to be the same. 这样,如果我们采用v
的第一列和u
的第一列,以及s
的第一个奇异值(左上角),则如果执行上述计算,则应使两个输出相同。
As an example: 举个例子:
rng(123);
A = randn(4,4);
[U,S,V] = svd(A);
format long;
o1 = A*V(:,1);
o2 = S(1,1)*U(:,1);
disp(o1);
disp(o2);
-0.267557887773137
1.758696945035771
0.934255531699997
-0.978346339659143
-0.267557887773136
1.758696945035771
0.934255531699996
-0.978346339659143
Similarly, if we looked at the second columns of U
and V
with the second singular value: 同样,如果我们用第二个奇异值查看U
和V
的第二列:
o1 = A*V(:,2);
o2 = S(2,2)*U(:,2);
disp(o1);
disp(o2);
0.353422275717823
-0.424888938462465
1.543570300948254
0.613563185406719
0.353422275717823
-0.424888938462465
1.543570300948252
0.613563185406719
As such, the basis vectors are arranged such that the columns are arranged from left to right in the same order as what the singular values dictate. 这样,基矢量被布置为使得列以与奇异值所指示的顺序相同的顺序从左到右布置。 As such, you simply grab the first 10 columns of U
. 这样,您只需获取U
的前10列。 This can be done by: 这可以通过以下方式完成:
out = U(:,1:10);
out
would contain the basis vectors, or columns of U
that correspond to the 10 highest singular values of A
. out
将包含基本向量,或列U
对应的10个最高奇异值A
。
First you sort the singular values, and save the reindexing, then take the first 10 values: 首先,对奇异值进行排序,并保存重新索引,然后获取前10个值:
[a, b]=sort(diag(S));
Umax10=U(:,b(1:10));
As mentioned by Rayryeng , svd
outputs the singular values in decreasing order so: 如Rayryeng所述 , svd
以降序输出奇异值,因此:
Umax10=U(:,1:10);
is enough. 足够。
Just have to remember that it is not the case for eig
, even though it may seems that eig
also outputs ordered eigenvalues it is not always the case. 只需记住,对于eig
并非如此,即使eig
似乎也输出有序特征值,却并非总是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.