0

I have just performed an SVD on my data (M = U.D.V^t), and I have the 3 svd matrices U, D, Vt. These matrices are sorted by the highest singular value of D (meaning the first column of U and first row of V corresponds to the highest singular value, etc.)

I want to swap this order according to a particular sort criteria : I am not interested in sorting by the absolute singular value but rather the singular value di multiplied by the first element of its corresponding vector in Vt

Example (pseudo-code, R code below) :

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, ... #  
]

The products sV[i]*Dti[1] give :

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

Which should be reordered descending [1,2,3] > [2,3,1]

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

... and propagate those changes to 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 Code

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,]
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • That doesn't look like R code. Are you using some pseudo code? It would help to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Feb 26 '16 at 22:43
  • Sorry yes I was using pseudocode for the description of the problem, I'm not very good with R but I guess it would work to generate a random matrix and perform the svd on it... – Cyril Duchon-Doris Feb 26 '16 at 22:46

1 Answers1

0

This is sorting a matrix you can easily find this on google searching for
r sorting matrix

Have a look here

Community
  • 1
  • 1
Etienne Moerman
  • 331
  • 1
  • 9
  • Thanks. In my case it was just a question of where to place the comma `theSVD$sortOrder <- theSVD$d*(theSVD$v[1,]) ; theSVD$v_r <- theSVD$v[,order(theSVD$sortOrder, decreasing=TRUE)]` ` – Cyril Duchon-Doris Mar 23 '16 at 20:53