sorting - F# - Sort a matrix containing tuples -


I did not find a way to sort the values ​​contained in the column of the following matrix of tuples: Matrix & lt; Float * float & gt; = Matrix [[(1.0, 145.0); (1.0, 45.0); (1.0, 130.0); (1.0, 30.0); (1.0, 130.0)] [(2.0, 45.0); (2.0, 45.0); (2.0, 30.0); (2.0, 30.0); (2.0, 30.0)] [(3.0, 130.0); (3.0, 30.0); (3.0, 145.0); (3.0, 45.0); (3.0, 130.0)] [(4.0, 30.0); (4.0, 30.0); (4.0, 45.0); (4.0, 45.0); (4.0, 30.0)] [(5.0, 130.0); (5.0, 30.0); (5.0, 130.0); (5.0, 30.0); I would like to sort each column on the basis of the second element of Tupal (5.0, 145.0)]]

Will be given:

  matrix [[(1.0, 145.0); (1.0, 45.0); (3.0, 145.0); (3.0, 45.0); (5.0, 145.0)] [(3.0, 130.0); (2.0, 45.0); (1.0, 130.0); (4.0, 45.0); (1.0, 130.0)] [(5.0, 130.0); (3.0, 30.0); (5.0, 130.0); (1.0, 30.0); (3.0, 130.0)] [(2.0, 45.0); (4.0, 30.0); (4.0, 45.0); (2.0, 30.0); (2.0, 30.0)] [(4.0, 30.0); (5.0, 30.0); (2.0, 30.0); (5.0, 30.0); Thank you in advance!   

P> In my experience, when arrays (2D and / Or matrix), then I came to know that working internally with arrays is often the fastest way.

For example, in the way of destabilizing the outlook of Daniel and Ankur:

  Let's start temporary astrobolic f (M: matrix & lt; 'T & gt; ) = Column = = [| 0 for C. .. m.NumCols - 1 - & gt; M. Column C | & Gt; Vector Generic Torre |] for C in 0 .. m.NumCols - 1 column. [C] | & Gt; Array.sortInPlace By f Matrix.Generic.init (m.NumRows) (m.NumCols) (Fun RC -> Columns. [C]. [R])   

Matrix columns (For an array of 'A [] [], no' [[]] and sorted the in-place on each column, after that, I filled out a new matrix with the sorted result. Note that the original matrix Cluttering: Column Array comes from the copies of the column vector (Vector Toure creates a new array).

This approach is fast because in it There is no need for transfers, instead of type of columns, and keeping all array-oriented, intermediate list structures do not require any conversions. I suspect that this can be made faster if the matrix module has' [ Even with conversion from [/] [], even though it is probably not really suitable for the matrix.

Also, know: you can compare the structural comparison of F # to the other Element can sort by descending, the first element of ascending:

Example:

  & gt; MutableSortByCol (Fun (A, B) - & gt; (-b, a)) ;; This value: Matrix & lt; Float * float & gt; = Matrix [[(1.0, 145.0); (1.0, 45.0); (3.0, 145.0); (3.0, 45.0); (5.0, 145.0)] [(3.0, 130.0); (2.0, 45.0); (1.0, 130.0); (4.0, 45.0); (1.0, 130.0)] [(5.0, 130.0); (3.0, 30.0); (5.0, 130.0); (1.0, 30.0); (3.0, 130.0)] [(2.0, 45.0); (4.0, 30.0); (4.0, 45.0); (2.0, 30.0); (2.0, 30.0)] [(4.0, 30.0); (5.0, 30.0); (2.0, 30.0); (5.0, 30.0); (4.0, 30.0)]]    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -