The compiler says it all.
In C, a 2D array is like an array of arrays. Therefore, a 2D array is fundamentally different from a 1D array; it is an array of pointers in which each element contains a pointer to an array (therefore, a bi-directional pointer, double** ).
You ask mxGetPr() to return double** , but it returns double* , for example, a pointer to the first element of an 1D array. This 1D array can only be indexed linearly.
My guess is that MATLAB does this in such a way as to support sequential indexing arrays - do you really expect / want a double**** for a 4-D array?
Furthermore, mxGetPr() cannot be overloaded with a return type (after all, C).
To be able to index the 1D array twice, you can sneak into a small macro:
and use it like that
double *A = mxGetPr(...); int numrows = 4; double blah = A(3,2);
Obviously, as with all macros, there are a few things to look for:
- no border check
- C is based on 0 and Matlab 1, which makes all indices different
- All arrays will need to be called "A"
You can write a function to address these shortcomings:
double getValue(double** array, int row, int* dims);
(or use mxCalcSingleSubscript as indicated by Shai ), but this does not improve the expressive power of IMHO:
double blah = getValue(array, 3,4, dims);
You can also write in C ++, create a class of type Matrix with operator() , build it using a pointer and dimensions from mxGetPr() and mxGetDims() , etc., compile in Matlab using g++ or equivalent, but this introduces a number of other problems and adds much greater complexity than is necessary for most cases.
Therefore, to avoid all this mess, I just always calculate the index in place :)