How convert ILArray into double[,] array
I have ILArray<double> A
want to use A.ToArray()
, but don't know how to use it
I need to get a double[,] System.Array
.
How convert ILArray into double[,] array
I have ILArray<double> A
want to use A.ToArray()
, but don't know how to use it
I need to get a double[,] System.Array
.
There is no natural support for converting an
ILArray<double>
into an multi dimensionalSystem.Array double[,]
in ILNumerics currently. So let's write it!This function should work on arbitrary
ILArray<T>
of arbitrary element type and arbitrary sizes / dimensions. (However, as always, you should do extensive testing before going productive!) It creates a new System.Array of the desired size and type and copies all elements in their natural (storage layout) order. The System.Array returned can get cast to the true multidimensional array afterwards.We use
Buffer.BlockCopy
in order to copy the elements. Keep in mind, System.Array stores its elements in row major order. ILNumerics (just like FORTRAN, Matlab and others) prefers column major order! So, since we just copy the elements quickly and do no efforts to reorder them in memory, the outcoming array will appear as having the dimensions flipped in comparison to the input array:exp
will be of size [3 x 4]. For matrices, this can easily be circumvented by transposing the input array:@Edit: bugfix: used Marshal.Sizeof(T) instead of sizeof(double) @Edit: bugfix: now fixed the fix: used Marshal.Sizeof(typeof(T))