通过自定义排序重新排序矩阵行

时间:2016-02-26 22:12:11

标签: r sorting matrix

我刚刚在我的数据上执行了一个SVD(M = UDV ^ t),我有3个svd矩阵U,D,Vt。这些矩阵按D的最高奇异值排序(意思是第一列U和第一行V对应于最高奇异值等。)

我想根据特定的排序标准交换这个顺序:我不想用绝对奇异值排序,而是奇异值di乘以Vt中相应向量的第一个元素

示例(伪代码,下面的R代码):

Singular_values =  [ sV[1]=100, sV[2]=1, sv[3]=50 ]

Dt = [
  0.1, xxx, ... # 1st row Dt1 associated to 1st singular Value
  100, yyy, ... # 2nd row Dt2 associated to 2nd singular Value
  1  , zzz, ... #  
]

产品sV[i]*Dti[1]给出:

100*0.1 = 10, # sV1*Dt1[1]
1*100 = 100,  # sV2*Dt2[1]
50*1 = 50     # sv3*Dt3[1]

哪个应该重新排序[1,2,3]> [2,3,1]

100  # sV2*Dt2[1]
50   # sv3*Dt3[1]
10   # sV1*Dt1[1]

...并将这些更改传播到Matrix Dt

Dt_reordered [
  100, yyy, ... # 2nd row Dt2 associated to 2nd singular Value
  1,   zzz, ... # 3rd row Dt3 associated to 3rd singular Value
  0.1, xxx, ... #  
]

R代码

dataToSVD = matrix(rexp(200), 10)
theSVD = svd(dataToSVD) # Generates ... 
# theSVD$u (Matrix U : I don't care about this one), 
# theSVD$d (List D : singularValues), 
# theSVD$v (Matrix V : Right singular vectors, not transposed)

theSVD$newValues <- theSVD$d*theSVD$v[1,] # This is a list of the "new" values that should be used for sorting
# The idea is now to sort theSVD$newValues by decreasing values, and the corresponding permutation must be applied also to theSVD$d and theSVD$v[1,]

1 个答案:

答案 0 :(得分:0)

这是对矩阵进行排序,您可以在谷歌搜索中轻松找到它     r排序矩阵

看看here