Array2D to Array

前端 未结 2 1542
不思量自难忘°
不思量自难忘° 2020-12-17 22:52

I have Array2D of 0 and 1:

let rnd = System.Random()
let a = Array2D.init n n (fun i j  -> int(System.Math.Round(rnd.NextDouble() / index)) )
2条回答
  •  执念已碎
    2020-12-17 23:17

    Here is one easy way, using the fact that [,] implements ienumerable<_>

    a |> Seq.cast |> Seq.filter (fun x -> x == 1)
    

    but if you only need the count you can do

    a |> Seq.cast |> Seq.sum
    

    as the 0 terms won't add to the sum and the terms you want to count are just 1

提交回复
热议问题