Array2D到Array [英] Array2D to Array

查看:85
本文介绍了Array2D到Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Array2D分别为0和1:

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)) )

我需要计算'1'元素的数量,例如:

I need to calculate the count of '1'-elements, something like:

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

但是'a'是Array2D(不是Array),所以我只是想知道是否存在将Array2D转换为Array的标准方法?

But 'a' is Array2D (not Array) so I'm just wondering if there is a standard way to transform Array2D to Array?

推荐答案

使用[,]实现ienumerable<_>

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

但是如果您只需要计数就可以

but if you only need the count you can do

a |> Seq.cast<int> |> Seq.sum

因为0项不会加到总和中,而您要计数的项仅为1

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

这篇关于Array2D到Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆