How to convert a matrix in matlab to a double array [,] in C # /. NET?

How can I convert a matrix of type MWArray (returned from the Matlab runtime) to a 2-dimensional array (double [,]) in C #?

I am working on the simplest Matlab and .NET platform as described at: http://domoreinlesstime.wordpress.com/2013/01/26/access-matlab-from-c/

Using the following statement, I can convert the result of a variable of type MWArray to a 1-dimensional array:

double[] arr = (double[])((MWNumericArray)result).ToVector(MWArrayComponent.Real);

Is there an easy way to convert the result to a 2 dimensional array in C #?

+4
source share
2 answers

Should be enough (worked for me):

double[,] arr = (double[,])((MWNumericArray)result).ToArray(MWArrayComponent.Real);
+6
source

Finally, I found the answer here:

matlab .NET. http://forum.finaquant.com/viewtopic.php?f=4&t=1217

double[] arr = (double[])((MWNumericArray)result).ToVector(MWArrayComponent.Real);

// convert 1-dim array (vector) to 2-dim array
double[] vec = (double[])((MWNumericArray)M).ToVector(MWArrayComponent.Real);
double[,] mat = new double[rows, cols];

for (int i = 0; i < cols; i++)
{
    for (int j = 0; j < rows; j++)
    {
        mat[j, i] = vec[i * cols + j + 1];
    }
}
-2

All Articles