F# how to append/join array2D and combine 1D arrays to array2D - f#

in F#, array.append can join two arrays; is there a way to append 2 array2D objects into one or column-wise join several 1-D arrays into one array2D object?

The array2D function will turn any seq<#seq<'T>> into a 'T [,] so it should work for turning a bunch of 1D arrays into a 2D array.
let arr1 = [| 1; 2; 3 |]
let arr2 = [| 4; 5; 6 |]
let combined = array2D [| arr1; arr2 |]

As far as I can see, there is no F# library function that would turn several 1D arrays into a single 2D array, but you can write your own function using Array2D.init:
let joinInto2D (arrays:_[][]) =
Array2D.init arrays.Length (arrays.[0].Length) (fun i j ->
arrays.[i].[j])
It takes an array of arrays, so when you call it, you'll give it an array containing all the arrays you want to join (e.g. [| arr1; arr2 |] to join two arrays). The function concatenates multiple 1D arrays into a single 2D array that contains one row for each input array. It assumes that the length of all the given arrays is the same (it may be a good idea to add a check to verify that, for example using Array.forall). The init function creates an array of the specified dimensions and then calls our lambda function to calculate a value for each element - in the lambda function, we use the row as an index in the array of arrays and the column as an index into the individual array.
Here is an example showing how to use the function:
> let arr1 = [| 1; 2; 3 |]
let arr2 = [| 4; 5; 6 |];;
> joinInto2D [| arr1; arr2 |];;
val it : int [,] = [[1; 2; 3]
[4; 5; 6]]
EDIT: There is already a function to do that - nice! I'll leave the answer here, because it may still be useful, for example if you wanted to join 1D arrays as columns (instead of rows, which is the behavior implemented by array2D.

I don't think there's anything built-in to handle this. You can define your own reusable method based on either Array2D.init or Array2D.blit, though. If you need to combine several columns into one logical whole, I think it would frequently be more convenient to use an array of arrays rather than a 2D array (and in general, the 1D array operations in .NET are significantly faster than the multi-dimensional array operations).

Related

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]|]
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.

plotting an array on F#, how to index axis by year

I would like to create create a graph of an array on F sharp as follows:
let compperformance= Array.zip firstarray secondarray |> Array.map (fun (a,b)
-> (b-a))
Chart.Line(compperformance, Title="dy").WithXAxis(Title="year", Min=1.0, Max=39.0)
.WithYAxis(Title="Cumulative SSE difference")
How can I change the x-axis to year (min=1960.0 to max=2000.0)? When I define min and max as such, the graph disappears. Let me know.
You can pass a sequence of key value pairs to Chart.Line:
let compperformance=
Array.zip firstarray secondarray |> Array.map (fun (a,b) -> (b-a))
let dataWithYears =
Array.zip [| 1960 .. 2000 |] compperformance
Chart.Line(dataWithYears, Title="dy")
.WithXAxis(Title="year", Min=1.0, Max=39.0)
.WithYAxis(Title="Cumulative SSE difference")
This assumes that there is exactly 41 values in compperformance, which might not quite work - you may need to do this a bit differently - but it shows the idea - just produce an array containing year, value tuples.

How do i write float values to a file in f#

i tried this following code what did i do wrong?
// Test IO
// Write a test file
let str : string[,] = Array2D.init 1 ASize (fun i j -> result.[i,j].ToString() )
System.IO.File.WriteAllLines(#"test.txt", str );
Will the first argument to Array2D.init in your code always be 1? If yes, then you can just create one dimensional array and it will work just fine:
let str = Array.init ASize (fun j -> result.[0,j].ToString() )
System.IO.File.WriteAllLines("test.txt", str );
If you really need to write a 2D array to a file, then you can convert 2D array into a one-dimensional array. The simplest way I can think of is this:
let separator = ""
let ar = Array.init (str.GetLength(0)) (fun i ->
seq { for j in 0 .. str.GetLength(1) - 1 -> str.[i, j] }
|> String.concat separator )
This generates a one-dimensional array (along the first coordinate) and then aggregates the elements along the second coordinate. It uses String.concat, so you can specify separator between the items on a single line.
because there are no overloads of File.WriteAllLines that accepts 2d array of strings. You should either convert it to 1d array or to seq<string>.

F# return sorting indices from array.sort

For an F# array, is there an easy way to return sorting indices along with the sorted array?
Like the sort() function from Matlab?
Background: I came from matlab/R, where manipulating array on indices are essential.
I'd like to reproduce some index functions and trying to be able to pass indices array around as a variable in various functions. for this sort() function, one usage would be sorting several aligned arrays together based on any one of them.
Maybe there are better ways to do this?
Use Array.sort in combination with Array.mapi.
let arr = [| 14;3;11;2 |]
let sortedWithIndex = arr |> Array.mapi (fun x t -> (t,x)) |> Array.sort
printfn "%A" sortedWithIndex
output
[|(2, 3); (3, 1); (11, 2); (14, 0)|]
let sortWithIndex list =
list
|> List.mapi (fun index x -> (x, index))
|> List.sort
(Depending on the version of F#, List.sort might require a comparison function like `compare ).

F# slicing array by indices array

How can I overload .[] for F# array to slice an array based on an arbitrary index array?
For example:
let x = [|1..10|];
let indx = [|4;1|];
although
[| for i in indx ->x.[i]|]
would work, it would be nicer to be able using x.[indx] directly.
You can always write an F# extension method to get close on the syntax
let a = [| 1..10 |]
let idx = [|4;1|]
type 'T ``[]`` with //'
member this.Slice(indices:int array) =
[| for i in indices -> this.[i] |]
printfn "%A" (a.Slice idx)
but since arrays already have an indexer it doesn't appear there's a way to overload/change it.

Resources