【问题标题】:Reorder matrix rows by custom sort通过自定义排序重新排序矩阵行
【发布时间】:2016-02-27 19:39:35
【问题描述】:

我刚刚对我的数据执行了 SVD (M = U.D.V^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,]

【问题讨论】:

  • 这看起来不像 R 代码。你在使用一些伪代码吗?制作reproducible example 会有所帮助。
  • 对不起,我使用伪代码来描述问题,我对 R 不是很好,但我想它可以生成一个随机矩阵并对其执行 svd...

标签: r sorting matrix


【解决方案1】:

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

看看here

【讨论】:

  • 谢谢。就我而言,这只是将逗号 theSVD$sortOrder &lt;- theSVD$d*(theSVD$v[1,]) ; theSVD$v_r &lt;- theSVD$v[,order(theSVD$sortOrder, decreasing=TRUE)] ` 放在哪里的问题
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 2021-02-13
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2011-03-02
相关资源
最近更新 更多