在f#中将相同长度的数组合并为一个2d数组 [英] Combining same length arrays into one 2d array in f#

查看:64
本文介绍了在f#中将相同长度的数组合并为一个2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在c#中问过这个问题,在这里,实际上并没有一种简洁的方法.我现在正在尝试在fsharp中执行相同的操作,然后看看效果如何.

I have asked this question before in c# here and there wasnt really a succinct way of doing this. I m now trying to do the same in fsharp and see how that goes.

我有两个长度和类型相同的数组,我想将它们合并为一个长度和2列相同的数组.我有执行此操作的代码:

I have two arrays that are same length and type and I would like to combine them into one with same length and 2 columns. I have this code that does this:

let twoDimensionalArray (arr1:array<'a>) (arr2:array<'a>) = 
    let rws = arr1|> Array.length
    Array2D.init rws 1 (fun i j -> arr1.[i], arr2.[i]) 

说实话,这让我感到有些惊讶,因为我本以为在列维中应该有一个2,如下所示:

To be honest this surprises me a bit as I would have thought there should be a 2 in the column dimension like this:

Array2D.init rws 2 (fun i j -> arr1.[i], arr2.[i]) 

但是如果我将其更改为2,我会得到:

but if I change this to 2 then I get this:

val it : (float * float) [,] = [[(1.0, 3.0); (1.0, 3.0)]
                            [(2.0, 4.0); (2.0, 4.0)]]

此数据:

let arr1 = [| 1.0; 2.0 |]
let arr2=  [|3.0; 4.0 |]

为什么?

我真的也想更宽泛地写这个,所以如果第一个数组有2列,第二个数组有3列,依此类推.我试过了:

Also I would really like to write this more generacally, so if the first array has 2 columns and the second 1, I get a 3 column matrix and so forth. I tried this:

let MultiArray (arr1:array<'a>) (arr2:array<'a>)  = 
    let rws = arr1.Length
    let c1 = arr1.GetLength(1)
    let c2 = arr2.GetLength(1)
    let cols = c1+c2
    Array2D.init rws cols (fun i j -> arr1.[i], arr2.[i]) 

但这是有缺陷的.

您能告诉我我该怎么做吗?也是为什么我的第一个功能可以正常工作但我认为是错误的?

Could you please let me know how I could do this? Also why my first function works but I think is wrong?

谢谢

谢谢约翰!工作解决方案:

Thanks John! Working solution:

let MultiArray (inp:float[][]) =
    let cls = inp |> Array.length
    let rows = inp.[0] |> Array.length
    Array2D.init rows cls (fun i j -> inp.[j].[i])

采样数据并使用:

let arr1 = [|1.0 ; 4.0; 6.0;5.;8.|] 
let arr2= [|7.0; 8.0; 9.0;9.;10. |] 
let inp = [| arr1; arr2; arr1|]

MultiArray inp;;
val it : float [,] = [[1.0; 7.0; 1.0]
                  [4.0; 8.0; 4.0]
                  [6.0; 9.0; 6.0]
                  [5.0; 9.0; 5.0]
                  [8.0; 10.0; 8.0]]

推荐答案

所以第一个代码的意外版本的原因是,您正在创建2D元组数组-而不是2D值数组.实际上,在这种情况下,您应该将第一维的大小设置为1,因为第二维隐藏在元组具有更多变量的事实中.

So the reason for the unexpected version of the first code is you are creating a 2D array of tuples - rather than a 2D array of values. You actually should set the size of the first dimension to 1 in this case as the second dimension is hiding in the fact that the tuple has more variables.

创建实际2D数组的简单版本应该是

A simple version which creates an actual 2D array would be

Array2D.init rws 2 (fun i j ->match j with |0 -> arr1.[i] |1 -> arr2.[i]) 

更通用的版本将像这样输入数组作为数组

A more generic version would take in an array of arrays as input like so

let MultiArray (inp:float[][])
    let count = inp |> Array.length
    let rows = inp.[0] |> Array.length
    Array2D.init count rows (fun i j -> inp.[j].[i]

这篇关于在f#中将相同长度的数组合并为一个2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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