Passing 3d arrays from .Net application to Matlab

9 views (last 30 days)
Hello. I try to integrate a Matlab code compiled as .Net assembly into C# program. The C# program needs to exchange data in a form of a 3d array with Matlab code. I can do it using an MWArray class objects. I have noticed though, that there is a dimension shift when trying to work with 3d arrays, such that if C# passes MxNxP array to Matlab, Matlab receives an PxMxN. There is no dimensions shift if passing 2d arrays.
I would like to know, if it is a known bug/feature, and is there a way to preserve dimension order when passing 3d arrays.
Thank you.

Answers (2)

Philip Caplan
Philip Caplan on 14 Apr 2015
Hi Evgeny, are you using the "ToArray" function? This function does change some properties while converting the array from MATLAB to .NET data type. This is because .NET follows a different convention from that of MATLAB.
1) The dimension ordering is changed from [i j k] to [k i j]. This is because MATLAB follows the column major order and .NET follows the row major order convention. But the rank of the matrix remains the same.
2) The index bound changes from [1,n] to [0,n-1]. The change in index bounds is expected, because in .NET the index starts with 0 and in MATLAB the index starts with 1.
For example, the index-bound from 1 to 10 in MATLAB becomes 0 to 9 in .NET.
Example:
Consider a 2x3x4 3D MWNumericArray ‘item1’. When you convert it into .NET array it becomes a 4x2x3 3D array. Let us call the converted array as ‘CArray’.
Now, suppose you change the value of MWNumeric array at location item1[1,3,2] to 100.
Now since [i j k] becomes [k i j], you would expect the value of CArray[2,1,3] to become 100.
But this is not the case. Since the index of .NET array starts with 0,
CArray[1,0,2] would be 123. I just subtracted 1 from each index and [2, 1, 3] became [1, 0, 2].
The following link explains the various data conversion rules that MATLAB follows while converting data from MATLAB to .NET data type or vice versa.

Evgeny Bespechansky
Evgeny Bespechansky on 15 Apr 2015
Thank you

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!