<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682</link>
    <title>MATLAB Central Newsreader - help regarding 2D matrix using c mex file</title>
    <description>Feed for thread: help regarding 2D matrix using c mex file</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 17 Aug 2009 07:59:02 -0400</pubDate>
      <title>help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#673654</link>
      <author>oruganti murthy</author>
      <description>Hi&lt;br&gt;
I am beginner and learning mex programming in C. &lt;br&gt;
I am trying to read a 2D matrix and print the pythagoras squares.&lt;br&gt;
Thought compiling is successful, MATALB has thrown exception.&lt;br&gt;
Can someone help me please...&lt;br&gt;
&lt;br&gt;
thanking you very much,&lt;br&gt;
&lt;br&gt;
with regards,&lt;br&gt;
ramana &lt;br&gt;
&lt;br&gt;
*********&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
#include &amp;lt;math.h&amp;gt;&lt;br&gt;
&lt;br&gt;
#define OUT        plhs[0]&lt;br&gt;
#define SAMPLE double   /* define the type used for data samples */&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Function Declarations                                                    */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
SAMPLE **get_matrix_pointers(int xDim, int yDim, SAMPLE *output);&lt;br&gt;
void release_matrix_pointers(SAMPLE **a);&lt;br&gt;
void py_dist(SAMPLE **a, int xDim, int yDim);&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Gateway function and error checking                                      */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;SAMPLE *output, **a;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int xDim, yDim;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;xDim = (int) mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;yDim = (int) mxGetN(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;/*do the matlab stuff*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;x Dimensions = %d.\n&quot;,xDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;y Dimensions = %d.\n&quot;,yDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;OUT = mxCreateDoubleMatrix(yDim, xDim, mxREAL); // mxArray is transpose of c matrix&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;output = mxGetPr(OUT);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a = mxGetPr(prhs[0]);  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;py_dist(a, xDim, yDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;release_matrix_pointers(a);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Free the space when we're done (just the array pointers)                 */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void release_matrix_pointers(SAMPLE **a)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxFree(a+1);&lt;br&gt;
} &lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Fill with pythagorian distances with the nice offset pointers            */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void py_dist(SAMPLE **a, int xDim, int yDim)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x,y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(x = 1; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (y = 1; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a[x][y] = sqrt((x*x)+(y*y));&lt;br&gt;
}&lt;br&gt;
************</description>
    </item>
    <item>
      <pubDate>Mon, 17 Aug 2009 08:18:02 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#673660</link>
      <author>Bruno Luong</author>
      <description>&quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6b2k6$oh2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt;    a = mxGetPr(prhs[0]);  &lt;br&gt;
&amp;gt; ...   &lt;br&gt;
&amp;gt; void py_dist(SAMPLE **a, int xDim, int yDim)&lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt;             a[x][y] = sqrt((x*x)+(y*y));&lt;br&gt;
&lt;br&gt;
I see at least one error. The &quot;a&quot; returned by mxGetPr is double*. It is a double simple-pointer of linear indexing and *not* 2D C-array.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 18 Aug 2009 09:41:02 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#673922</link>
      <author>oruganti murthy</author>
      <description>Thank you very much Bruno.&lt;br&gt;
I changed it to &lt;br&gt;
****&lt;br&gt;
void py_dist(SAMPLE **a, int xDim, int yDim, SAMPLE **output)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x,y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*output++ = *a++ ;&lt;br&gt;
}&lt;br&gt;
********&lt;br&gt;
and now I'm able to read the 2D array.&lt;br&gt;
But how to overcome the Transposed form of reading array from MATLAB...?&lt;br&gt;
&lt;br&gt;
with regards,&lt;br&gt;
ramana</description>
    </item>
    <item>
      <pubDate>Tue, 18 Aug 2009 09:56:35 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#673925</link>
      <author>Rune Allnor</author>
      <description>On 17 Aug, 09:59, &quot;oruganti murthy&quot; &amp;lt;omur...@yahoo.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi&lt;br&gt;
&amp;gt; I am beginner and learning mex programming in C.&lt;br&gt;
&lt;br&gt;
If you mean that you learn C by programming MEX files,&lt;br&gt;
then you might consider dropping the MEX interfaces for&lt;br&gt;
now and use a C IDE to learn C on its own terms first.&lt;br&gt;
&lt;br&gt;
There are some free IDEs around, like Visual Studio Express&lt;br&gt;
Edition.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Tue, 18 Aug 2009 09:57:02 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#673927</link>
      <author>Bruno Luong</author>
      <description>&quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6dsve$37p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; But how to overcome the Transposed form of reading array from MATLAB...?&lt;br&gt;
&lt;br&gt;
I guess you don't have other choice than transposing the matrix - either in C or in Matlab side. The &quot;issue&quot; is due the way Matlab organizes the data. If your function work with the opposite convention then you have convert back and forth.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 18 Aug 2009 18:48:02 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674054</link>
      <author>James Tursa</author>
      <description>&quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6dsve$37p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thank you very much Bruno.&lt;br&gt;
&amp;gt; I changed it to &lt;br&gt;
&amp;gt; ****&lt;br&gt;
&amp;gt; void py_dist(SAMPLE **a, int xDim, int yDim, SAMPLE **output)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;     int x,y;&lt;br&gt;
&amp;gt;     for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;gt;         for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;gt;             *output++ = *a++ ;&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; ********&lt;br&gt;
&amp;gt; and now I'm able to read the 2D array.&lt;br&gt;
&amp;gt; But how to overcome the Transposed form of reading array from MATLAB...?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
This doesn't look correct at all. I suspect you still have bugs in the code if this is what you are currently using. Would you mind posting your current complete routine for us to look at?&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Wed, 19 Aug 2009 00:35:04 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674114</link>
      <author>oruganti murthy</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguy_with_a_k_not_a_c@hotmail.com&amp;gt; wrote in message &amp;lt;h6et12$284$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6dsve$37p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Thank you very much Bruno.&lt;br&gt;
&amp;gt; &amp;gt; I changed it to &lt;br&gt;
&amp;gt; &amp;gt; ****&lt;br&gt;
&amp;gt; &amp;gt; void py_dist(SAMPLE **a, int xDim, int yDim, SAMPLE **output)&lt;br&gt;
&amp;gt; &amp;gt; {&lt;br&gt;
&amp;gt; &amp;gt;     int x,y;&lt;br&gt;
&amp;gt; &amp;gt;     for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;gt; &amp;gt;         for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;gt; &amp;gt;             *output++ = *a++ ;&lt;br&gt;
&amp;gt; &amp;gt; }&lt;br&gt;
&amp;gt; &amp;gt; ********&lt;br&gt;
&amp;gt; &amp;gt; and now I'm able to read the 2D array.&lt;br&gt;
&amp;gt; &amp;gt; But how to overcome the Transposed form of reading array from MATLAB...?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This doesn't look correct at all. I suspect you still have bugs in the code if this is what you are currently using. Would you mind posting your current complete routine for us to look at?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&lt;br&gt;
Hi all,&lt;br&gt;
Thank you for your different replies.&lt;br&gt;
Sending the transpose of a matrix from MAtlab seems suitable solution to me. Very simple but didn't struck to me. Thank you bruno.&lt;br&gt;
dear James,&lt;br&gt;
Here is the code and the output I've got.&lt;br&gt;
Please let me know if I can improve it any more&lt;br&gt;
&lt;br&gt;
*************&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
#include &amp;lt;math.h&amp;gt;&lt;br&gt;
&lt;br&gt;
#define OUT        plhs[0]&lt;br&gt;
#define SAMPLE double   /* define the type used for data samples */&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Function Declarations                                                    */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
SAMPLE **get_matrix_pointers(int xDim, int yDim, SAMPLE *output);&lt;br&gt;
void release_matrix_pointers(SAMPLE **a);&lt;br&gt;
void py_dist(SAMPLE **a, int xDim, int yDim);&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Gateway function and error checking                                      */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;SAMPLE **output, **a;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int xDim, yDim;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;xDim = (int) mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;yDim = (int) mxGetN(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;/*do the matlab stuff*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;x Dimensions = %d.\n&quot;,xDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;y Dimensions = %d.\n&quot;,yDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;OUT = mxCreateDoubleMatrix(yDim, xDim, mxREAL); // mxArray is transpose of c matrix&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;output = mxGetPr(OUT);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a = mxGetPr(prhs[0]);  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;py_dist(a, xDim, yDim, output);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;release_matrix_pointers(a);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Free the space when we're done (just the array pointers)                 */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void release_matrix_pointers(SAMPLE **a)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxFree(a+1);&lt;br&gt;
} &lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
/* Fill with pythagorian distances with the nice offset pointers            */&lt;br&gt;
/****************************************************************************/&lt;br&gt;
void py_dist(SAMPLE **a, int xDim, int yDim, SAMPLE **output)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x,y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*output++ = *a++ ;&lt;br&gt;
}&lt;br&gt;
%%%%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
with regards,&lt;br&gt;
ramana&lt;br&gt;
&amp;gt;&amp;gt; mex Demo3.c&lt;br&gt;
&amp;gt;&amp;gt; a = [1 2 3; 3 4 5];&lt;br&gt;
&amp;gt;&amp;gt; Demo3(a)&lt;br&gt;
x Dimensions = 2.&lt;br&gt;
y Dimensions = 3.&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     5</description>
    </item>
    <item>
      <pubDate>Wed, 19 Aug 2009 07:52:01 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674154</link>
      <author>James Tursa</author>
      <description>&quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6fhbo$l34$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Here is the code and the output I've got.&lt;br&gt;
&amp;gt; Please let me know if I can improve it any more&lt;br&gt;
&lt;br&gt;
The code has some very serious errors. I am surprised you got anything reasonable for output. It corrupts memory and crashes my machine. Here are my comments:&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; *************&lt;br&gt;
&amp;gt; #include &quot;mex.h&quot;&lt;br&gt;
&amp;gt; #include &amp;lt;math.h&amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; #define OUT        plhs[0]&lt;br&gt;
&amp;gt; #define SAMPLE double   /* define the type used for data samples */&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; /* Function Declarations                                                    */&lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; SAMPLE **get_matrix_pointers(int xDim, int yDim, SAMPLE *output);&lt;br&gt;
&lt;br&gt;
Can't comment on this one since you didn't show the code for this function. But I strongly suspect that it is incorrect based on your SAMPLE ** return type. See discussion below.&lt;br&gt;
&lt;br&gt;
&amp;gt; void release_matrix_pointers(SAMPLE **a);&lt;br&gt;
&lt;br&gt;
Get rid of this ... see comments below.&lt;br&gt;
&lt;br&gt;
&amp;gt; void py_dist(SAMPLE **a, int xDim, int yDim);&lt;br&gt;
&lt;br&gt;
This prototype doesn't match your function. The prototype has only three arguments, while your function definition has four. You would have seen a compiler warning had you used the -v option with the mex command. Also, based on your intended usage there are too many levels of indirection for &quot;a&quot;.&lt;br&gt;
&lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; /* Gateway function and error checking                                      */&lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])&lt;br&gt;
&amp;gt; {&lt;br&gt;
&lt;br&gt;
Note that the prhs points to a const mxArray ... i.e. it is intended to be read-only.&lt;br&gt;
&lt;br&gt;
&amp;gt;    SAMPLE **output, **a;&lt;br&gt;
&lt;br&gt;
No, this is bad. These are being used as pointers to data areas, so there should only be a single level of indirection. i.e., the mxGetPr returns a double *, not a double **. So you should have this instead:&lt;br&gt;
&lt;br&gt;
SAMPLE *output, *a;&lt;br&gt;
&lt;br&gt;
&amp;gt;    int xDim, yDim;&lt;br&gt;
&amp;gt;    &lt;br&gt;
&amp;gt;    xDim = (int) mxGetM(prhs[0]);&lt;br&gt;
&amp;gt;    yDim = (int) mxGetN(prhs[0]);&lt;br&gt;
&lt;br&gt;
Moot point for most inputs, but be advised that later versions of MATLAB the return type of mxGetM and mxGetN is not int but mwSize, which may be an unsigned int. So if you happen to get a very large matrix as input that has a size larger than the largest int, the result of these lines will overflow the int and produce implementation dependent results. On 2's complement machines, that usually means modulo result, meaning the overflow will wrap around to negative values and you will end up with xDim and yDim being negative. Better to stick with the return type of these functions. e.g.,&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwSize xDim, yDim;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xDim = mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yDim = mxGetN(prhs[0]);&lt;br&gt;
&lt;br&gt;
&amp;gt;    /*do the matlab stuff*/&lt;br&gt;
&amp;gt;    mexPrintf(&quot;x Dimensions = %d.\n&quot;,xDim);&lt;br&gt;
&amp;gt;    mexPrintf(&quot;y Dimensions = %d.\n&quot;,yDim);&lt;br&gt;
&lt;br&gt;
And if you make the change to mwSize above, then use an unsigned int format here. e.g., %u instead of %d.&lt;br&gt;
&lt;br&gt;
&amp;gt;    OUT = mxCreateDoubleMatrix(yDim, xDim, mxREAL); // mxArray is transpose of c matrix&lt;br&gt;
&lt;br&gt;
I think you missed the point about the transpose stuff. In MATLAB, memory for a 2D array is stored in column order, whereas in C memory for a 2D matrix is stored in row order. So if you take a 2x3 matrix from MATLAB and do a block memory copy into a matrix in C and want to access the elements in C using the [ ][ ] notation, you need to treat the C variable as a 3x2 matrix that is the transpose of the MATLAB matrix. My advice is to not use the [ ][ ] notation. Just use a double * and do your own indexing calculations to match the MATLAB column ordering of the data. It is easier to keep things straight in your code this way, and avoids the extra code needed to set up the [ ][ ] access syntax. So no need to switch xDim and yDim.&lt;br&gt;
&lt;br&gt;
&amp;gt;    output = mxGetPr(OUT);&lt;br&gt;
&amp;gt;    a = mxGetPr(prhs[0]);  &lt;br&gt;
&amp;gt;    py_dist(a, xDim, yDim, output);&lt;br&gt;
&lt;br&gt;
More arguments than prototype as mentioned above.&lt;br&gt;
&lt;br&gt;
&amp;gt;    release_matrix_pointers(a);&lt;br&gt;
&lt;br&gt;
This is very bad. You should *never* free anything associated with an input mxArray such as prhs[0]. In this case, you are passing the result of the a = mxGetPr(prhs[0]) to your routine and then subsequently calling free(a+1). First, a+1 is an invalid address to pass back to free so the result of the free(a+1) call is undefined. Not sure what you intended here. Maybe nothing bad will happen, or maybe corrupt memory and program crash. 2nd, as pointed out above, you shouldn't be doing this anyway since &quot;a&quot; is associated with a const (i.e., intended to be read-only) input. Get rid of this line.&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
&amp;gt; /* Free the space when we're done (just the array pointers)                 */&lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; void release_matrix_pointers(SAMPLE **a)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;     mxFree(a+1);&lt;br&gt;
&amp;gt; } &lt;br&gt;
&lt;br&gt;
Get rid of this routine.&lt;br&gt;
&lt;br&gt;
/****************************************************************************/&lt;br&gt;
&amp;gt; /* Fill with pythagorian distances with the nice offset pointers            */&lt;br&gt;
&amp;gt; /****************************************************************************/&lt;br&gt;
&amp;gt; void py_dist(SAMPLE **a, int xDim, int yDim, SAMPLE **output)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;     int x,y;&lt;br&gt;
&amp;gt;     for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;gt;         for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;gt;             *output++ = *a++ ;&lt;br&gt;
&amp;gt; }&lt;br&gt;
&lt;br&gt;
This is amazing, at least to me, that it worked as well as it did. You are passing in a (double *), recast as a (double **). Then this line does a single dereference and copy:&lt;br&gt;
&lt;br&gt;
&amp;gt;             *output++ = *a++ ;&lt;br&gt;
&lt;br&gt;
The result of the *output operation is a (double *) type. So you are doing this type assignment:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(double *) = (double *);&lt;br&gt;
&lt;br&gt;
i.e., you are assigning a double * to another double *, even though the underlying data is double, not double *. So it seems to me that an incorrect number of bytes is being copied, and as an incorrect type to boot. Here is what you should be doing (getting rid of one level of indirection on &quot;a&quot; and &quot;output&quot;):&lt;br&gt;
&lt;br&gt;
&amp;nbsp;void py_dist(SAMPLE *a, int xDim, int yDim, SAMPLE *output)&lt;br&gt;
&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x,y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*output++ = *a++ ;&lt;br&gt;
&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
Finally, if you are just doing an element-by-element calculation, it is easier to use mxGetNumberOfElements and use a single for loop to access all the elements linearly.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Thu, 20 Aug 2009 08:41:03 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674469</link>
      <author>oruganti murthy</author>
      <description>Dear James,&lt;br&gt;
Thank you very much for your detailed steps. &lt;br&gt;
I have made the changes and here is the output I obtained.&lt;br&gt;
I actually wanted my output (for some later computations in C) the *output to be format output[][]. How can I obtain that?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
with regards,&lt;br&gt;
ramana&lt;br&gt;
*********&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
#include &amp;lt;math.h&amp;gt;&lt;br&gt;
&lt;br&gt;
#define OUT        plhs[0]&lt;br&gt;
void py_dist(double *a, mwSize xDim, mwSize yDim, double *output) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x, y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(x = 0; x&amp;lt;=xDim; ++x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (y = 0; y&amp;lt;=yDim; ++y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ double t = *a++;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*output++ =  t*t;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *output, *a;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwSize xDim, yDim;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xDim = mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yDim = mxGetN(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*do the matlab stuff*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;x Dimensions = %u.\n&quot;, xDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;y Dimensions = %u.\n&quot;, yDim);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OUT = mxCreateDoubleMatrix(xDim, yDim, mxREAL); // mxArray is transpose of c matrix&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output = mxGetPr(OUT);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = mxGetPr(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;py_dist(a, xDim, yDim, output);&lt;br&gt;
}&lt;br&gt;
********&lt;br&gt;
&amp;gt;&amp;gt; mex Demo3.c&lt;br&gt;
&amp;gt;&amp;gt; a = [1 2 3; 3 4 5];&lt;br&gt;
&amp;gt;&amp;gt; Demo3(a)&lt;br&gt;
x Dimensions = 2.&lt;br&gt;
y Dimensions = 3.&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     4     9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;9    16    25</description>
    </item>
    <item>
      <pubDate>Thu, 20 Aug 2009 09:18:18 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674477</link>
      <author>Bruno Luong</author>
      <description>&quot;oruganti murthy&quot; &amp;lt;omurthy@yahoo.com&amp;gt; wrote in message &amp;lt;h6j26v$n34$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dear James,&lt;br&gt;
&amp;gt; Thank you very much for your detailed steps. &lt;br&gt;
&amp;gt; I have made the changes and here is the output I obtained.&lt;br&gt;
&amp;gt; I actually wanted my output (for some later computations in C) the *output to be format output[][]. How can I obtain that?&lt;br&gt;
&lt;br&gt;
C-2D are pointer array (level1) of pointers (level2); each of the pointer (level2) is 1D array.&lt;br&gt;
&lt;br&gt;
So if you want to convert Matlab linear indexing array to C 2D-array you need to do the following.&lt;br&gt;
&lt;br&gt;
- Allocate an array of pointers (level1) of the length equal to the number of (Matlab) columns in the data.&lt;br&gt;
- Assign the value of each jth pointer element (level2) as the address of the first element of column #j.&lt;br&gt;
&lt;br&gt;
After using it, don't forget to free the array level1.&lt;br&gt;
&lt;br&gt;
Remember, C and Matlab arrays are transposed to each other.&lt;br&gt;
&lt;br&gt;
Try this:&lt;br&gt;
&lt;br&gt;
% On Matlab command line:&lt;br&gt;
&lt;br&gt;
mex c2d.c&lt;br&gt;
c2d([1 2 3; 4 5 6]);&lt;br&gt;
&lt;br&gt;
/* Mex test program c2d.c */&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double **Carray; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *data;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwSize m, n, i, j;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data = mxGetPr(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m = mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n = mxGetN(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Create C 2D array */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Carray = mxCalloc(n, sizeof(double*));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (j=0; j&amp;lt;n; j++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Carray[j] = data+m*j;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Use it */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (i=0; i&amp;lt;m; i++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (j=0; j&amp;lt;n; j++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexPrintf(&quot;data(%i,%i) = %f\n&quot;, i+1, j+1, Carray[j][i]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxFree(Carray);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
/* Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 20 Aug 2009 09:37:02 -0400</pubDate>
      <title>Re: help regarding 2D matrix using c mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258682#674482</link>
      <author>oruganti murthy</author>
      <description>Hi Bruno,&lt;br&gt;
Excellent code!&lt;br&gt;
I got&lt;br&gt;
********&lt;br&gt;
&amp;gt;&amp;gt; mex c2d.c&lt;br&gt;
&amp;gt;&amp;gt; c2d([1 2 3; 4 5 6]);&lt;br&gt;
data(1,1) = 1.000000&lt;br&gt;
data(1,2) = 2.000000&lt;br&gt;
data(1,3) = 3.000000&lt;br&gt;
data(2,1) = 4.000000&lt;br&gt;
data(2,2) = 5.000000&lt;br&gt;
data(2,3) = 6.000000&lt;br&gt;
*******&lt;br&gt;
Thank you very much. It serves my purpose.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
with regards,&lt;br&gt;
ramana </description>
    </item>
  </channel>
</rss>

