SciSharp / NumSharp

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.
https://github.com/SciSharp
Apache License 2.0
1.36k stars 189 forks source link

How to convert a matrix to a multidimension array? #402

Closed Sullivanecidi closed 4 years ago

Sullivanecidi commented 4 years ago

Hello Again! Last time I asked that: how to convert a NDArray to a list? It can be implemented just as ToArray(). So, how about a matrix to an array? Say matrix[4,5] to double[,]?

Nucs commented 4 years ago

No builtin way to do that: heres a snippet that should work for your case

unsafe double[,] To2DArray(NDArray nd)
{
    var ret = new double[nd.shape[0], nd.shape[1]];
    fixed (double* dst = ret)
        Unsafe.CopyBlock(dst, nd.Unsafe.Address, (uint)(nd.size * sizeof(double)));

    return ret;
}