A simple way to swap rows in a matrix for F #

Is there an easy way to swap matrix rows in F #?

+3
source share
2 answers

The section syntax can be used to control entire rows / columns of a matrix:

// Create sample matrix
let m = Matrix.init 10 10 (fun x y -> float(x * 10 + y))
// Overwrite first row with the second row
m.[0..0, 0..9] <- m.[1..1, 0..9]

The slicing syntax allows you to select a part of the matrix - in this case, we select a matrix with a height of 1, but you can use this function as a whole (the part does not have to be a single column row). I do not think that there is any existing function for exchanging two strings, but you can use slices and implement it as follows:

let swap (m:matrix) a b = 
  let tmp = m.[a..a, 1..9]
  m.[a..a, 1..9] <- m.[b..b, 1..9]
  m.[b..b, 1..9] <- tmp
+2
source

- PermuteRows Matrix:

let m = Matrix.init 10 10 (fun x y -> float(x * 10 + y))
let m2 = m.PermuteRows (fun i -> 9 - i) 

, (i -> 9 - i).

+2

Source: https://habr.com/ru/post/1747895/


All Articles