HaagenDaz
HaagenDaz

Reputation: 461

Array of arrays in F#

I have the following array CF which contains arrays. How can I set up another array of same length as CF which finds the nth element of arrays within CF.

let CF=   [|for i in 0..(Connection.Length-1)-> 
                          res RProject.[i] rc.[i] D.[i] in.[i]|]

Upvotes: 0

Views: 502

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233505

If I understand the question correctly, you want the nth element of each array, mapped to a new array. You can do this with Array.map.

Here's an example. First, an array of arrays:

let a1 = [| [|1; 2; 3|]; [| 4; 5; 6 |] |]

Let's say you want the second element in each of these arrays, mapped into a new array. This can be done like this:

let a2 = a1 |> Array.map (fun a -> [| a.[1] |])

In this example, a2 will now have the value [|[|2|]; [|5|]|].

Notice that an exception will be thrown if you attempt to access an array index that is outside the bounds of any of the arrays.

Upvotes: 2

Related Questions