5

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

I am working on the simplest matlab & .NET integration as explained in: http://domoreinlesstime.wordpress.com/2013/01/26/access-matlab-from-c/

With the following statement I can convert the variable result of type MWArray to a 1-dim array:

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

Is there a simple way to convert result into a 2-dim array in C#?

2 Answers 2

7

The following should be enough (worked for me):

double[,] arr = (double[,])((MWNumericArray)result).ToArray(MWArrayComponent.Real);
Sign up to request clarification or add additional context in comments.

Comments

-2

Finally, I found the answer here:

calling matlab functions from .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];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.