title: How to loop through matrix elements in mex c++ function for MATLAB?
body: <p>I am trying to index write an external c++ function for MATLAB using mex to manipulate matrices, and am not able to use multidimensional indexing. There are examples provided <a href="http://www.mathworks.co.uk/help/matlab/matlab_external/table-of-mex-examples.html" rel="noreferrer">here</a>, but I have not found how to fix the problem I describe below.
  I have a sample matrix:</p>
  
  <pre><code>&gt;&gt; mat
  mat =
   1    10
   2    20
   3    30
   4    40
   5    50
  </code></pre>
  
  <p>Currently I use a linear index through the matrix which works:</p>
  
  <pre><code>#include &lt;mex.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
  { 
   //1.get pointer to input graph_list and allocate it    
      double *graph_list = mxGetPr(prhs[0]);
      mwSize mrows = mxGetM(prhs[0]);
      mwSize ncols = mxGetN(prhs[0]);  
      cout&lt;&lt; mrows&lt;&lt;" rows\n";
      cout&lt;&lt; ncols&lt;&lt;" cols\n";
      int mm, nn;
      for (nn=0;nn&lt;ncols;nn++) {
          for (mm=0;mm&lt;mrows;mm++){
              cout &lt;&lt; graph_list[nn*(mrows) +mm]  &lt;&lt;"\n";            
          }
      }     
  }
  </code></pre>
  
  <p>This produces: </p>
  
  <pre><code>&gt;&gt; mexTryAlex(mat)    
  5 rows
  2 cols
  1
  2
  3
  4
  5
  10
  20
  30
  40
  50
  </code></pre>
  
  <p>When I change the definition of graph_list and try 2D indexing to graph_list there is a  compilation error with <code>mex</code>:</p>
  
  <pre><code>double **graph_list = mxGetPr(prhs[0]);
  cout &lt;&lt; graph_list[nn][mm];
  </code></pre>
  
  <p>EDIT: here is the error message received</p>
  
  <pre><code>&gt;&gt; mex mexTryAlex.cpp
  Warning: You are using gcc version "4.4.3-4ubuntu5)".  The version
       currently supported with MEX is "4.3.4".
       For a list of currently supported compilers see: 
       http://www.mathworks.com/support/compilers/current_release/
  mexTryAlex.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
  mexTryAlex.cpp:16: error: cannot convert ‘double*’ to ‘double**’ in initialization
  mex: compile of ' "mexTryAlex.cpp"' failed.
  ??? Error using ==&gt; mex at 208
  Unable to complete successfully.
  </code></pre>