0

I've written a function in F# which which uses the KMeans (http://accord-framework.net/docs/html/T_Accord_MachineLearning_KMeans.htm).

A function called compute requires float[][] but I've never seen that in F#. only float[,].

Example of code is below.

let ConverterForCluster =

I create a zero based array of 50 x 50. This will be populated once I interface this with an existing application that returns a float[,], at the moment I'm trying to get to write a converter from [,] to [][]

My distance array is declared as

    let mutable Distance = Array2D.zeroCreate 50 50

KMeans is a method from Accord.NET library (link at the top).

    let km = KMeans(5)

I'm creating a new variable called dm just so that function Compute of km (below) can accept it. However, using Unchecked.defaultof creates a null array which is the error

    let dm : float[][] = Unchecked.defaultof<float[][]>

    for i = 0 to 49 do
        for j = 0 to 49 do

I believe this conversion from float[,] to float[][] is fine but I've yet to initialise dm so I can't confirm that 100%

    dm.[i].[j] <- Convert.ToDouble Distance.[i, j]

This is the function that needs float[][] and won't accept [,]

    let label = km.Compute(dm)

    label

The exception is:

System.NullReferenceException was unhandled Message: An unhandled exception of type 'System.NullReferenceException' occurred in Cluster.exe Additional information: Object reference not set to an instance of an object.

This error is on the following line

dm.[i].[j] <- Convert.ToDouble Distance.[i, j]

because dm is null due to the line below as I don't know how to initialise it

let dm : float[][] = Unchecked.defaultof<float[][]>

My question is how do I initialise this type of array? And what is the difference?

If I've missed anything from this, please let me know or if it doesn't make sense. Thanks.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Silent Fart
  • 111
  • 2
  • 13
  • In short `float[,]`is a [multidimensional array](https://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays) and `float[][]` is a [jagged array](https://en.wikipedia.org/wiki/Jagged_array). – Guy Coder Mar 24 '16 at 13:07
  • T[][] Is just an array of arrays, as T[,] in fact a one-dimensional array. – FoggyFinder Mar 24 '16 at 13:09
  • I am voting to close as a duplicate because both questions are answered in the other question. What is the difference is in the answer, and how to initialize is answered in the F# WikiBooks link. You can mark this question as a duplicate yourself which will instantly change it's status. :) – Guy Coder Mar 24 '16 at 13:16
  • @Guy Coder, Or just remove, right? – FoggyFinder Mar 24 '16 at 13:19
  • 1
    @FoggyFinder He could remove it, but sometimes leaving them marked as duplicate is like leaving bread crumbs for others to find their way to the original answer. For this one I would say leave it here and mark as duplicate because it has additional information not found in the original question. – Guy Coder Mar 24 '16 at 13:21
  • @GuyCoder Ah that's what they're called... Thanks :) I'll mark it as duplicate. – Silent Fart Mar 24 '16 at 13:42
  • Solved the initialising issue by doing let dm : float[][] = [| for a in 0 .. 49 do yield [| 0.0 .. 49.0 |] |] Although it creates a jagged array with values other zero but oh well... – Silent Fart Mar 24 '16 at 13:43

1 Answers1

-3
let dm : float[][] = [| for a in 0 .. 49 do yield [| 0.0 .. 49.0 |] |]
Silent Fart
  • 111
  • 2
  • 13