Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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#?

like image 904
selmar Avatar asked Nov 16 '13 15:11

selmar


People also ask

How do you create a double array in MATLAB?

You create a double-precision array automatically when you assign a numeric scalar or array to a variable, such as A = [1 2 3; 4 5 6] . The variable A has type double .

How do you flatten a matrix to a vector in MATLAB?

Conversion of a Matrix into a Row Vector. This conversion can be done using reshape() function along with the Transpose operation. This reshape() function is used to reshape the specified matrix using the given size vector.

What is a 1x2 double in MATLAB?

Each element of the 2nd row is a cell array. And each of those cell arrays has two elements (1x2 dimension). Each of those two elements is in and of itself a MATLAB double variable.


1 Answers

The following should be enough (worked for me):

double[,] arr = (double[,])((MWNumericArray)result).ToArray(MWArrayComponent.Real);
like image 147
Arjan Avatar answered Sep 22 '22 17:09

Arjan