Quantcast
Channel: Math.NET Numerics
Viewing all articles
Browse latest Browse all 971

New Post: On the subject of permutations...

$
0
0
Perhaps I've missed it but I don't believe Numerics has a create random permutation function.
I've had need of randomly permuting columns/rows in matrices and so I wrote this short little function to create the random permutations.
It is an implementation of the Fisher-Yates shuffle.
 Public Function RandPermutation(ByVal length As Integer) As Permutation
    'This function returns a random permutation mapping of length 'length'.
    'For use in the permute rows/columns matrix functions
    Randomize()
    Dim series(length - 1) As Integer
    Dim randint, temp As Integer
    For x = 0 To length - 1
        series(x) = x
    Next
    For i = length - 1 To 1 Step -1
        randint = Int(Rnd() * (i) + 0.5)
        temp = series(i)
        series(i) = series(randint)
        series(randint) = temp
    Next
    Return Permutation.FromInversions(series)
End Function
It is nice that you can store this permutation and then use it again for another matrix.
For example if you have 2 matrices with corresponding rows/columns you can permute each with the same permutation and the matrices will still correspond.
This is useful for me in that I have matrices of input variables and corresponding matrices of target values. When doing mini-batch gradient descent I can use the random permutation function without losing the correspondence between input and target.

Viewing all articles
Browse latest Browse all 971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>