<html lang="en"> <head> <title>Working with Matrices and Arrays in Mex-Files - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Mex_002dFiles.html#Mex_002dFiles" title="Mex-Files"> <link rel="prev" href="Getting-Started-with-Mex_002dFiles.html#Getting-Started-with-Mex_002dFiles" title="Getting Started with Mex-Files"> <link rel="next" href="Character-Strings-in-Mex_002dFiles.html#Character-Strings-in-Mex_002dFiles" title="Character Strings in Mex-Files"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Working-with-Matrices-and-Arrays-in-Mex-Files"></a> <a name="Working-with-Matrices-and-Arrays-in-Mex_002dFiles"></a> <p> Next: <a rel="next" accesskey="n" href="Character-Strings-in-Mex_002dFiles.html#Character-Strings-in-Mex_002dFiles">Character Strings in Mex-Files</a>, Previous: <a rel="previous" accesskey="p" href="Getting-Started-with-Mex_002dFiles.html#Getting-Started-with-Mex_002dFiles">Getting Started with Mex-Files</a>, Up: <a rel="up" accesskey="u" href="Mex_002dFiles.html#Mex_002dFiles">Mex-Files</a> <hr> </div> <h4 class="subsection">A.2.2 Working with Matrices and Arrays in Mex-Files</h4> <p>The basic mex type of all variables is <code>mxArray</code>. All variables, such as matrices, cell arrays or structures are all stored in this basic type, and this type serves basically the same purpose as the octave_value class in oct-files. That is it acts as a container for the more specialized types. <p>The <code>mxArray</code> structure contains at a minimum, the variable it represents name, its dimensions, its type and whether the variable is real or complex. It can however contain a number of additional fields depending on the type of the <code>mxArray</code>. There are a number of functions to create <code>mxArray</code> structures, including <code>mxCreateCellArray</code>, <code>mxCreateSparse</code> and the generic <code>mxCreateNumericArray</code>. <p>The basic functions to access the data contained in an array is <code>mxGetPr</code>. As the mex interface assumes that the real and imaginary parts of a complex array are stored separately, there is an equivalent function <code>mxGetPi</code> that get the imaginary part. Both of these functions are for use only with double precision matrices. There also exists the generic function <code>mxGetData</code> and <code>mxGetImagData</code> that perform the same operation on all matrix types. For example <pre class="example"> mxArray *m; mwSize *dims; UINT32_T *pr; dims = (mwSize *) mxMalloc (2 * sizeof(mwSize)); dims[0] = 2; dims[1] = 2; m = mxCreateNumericArray (2, dims, mxUINT32_CLASS, mxREAL); pr = = (UINT32_T *) mxGetData (m); </pre> <p>There are also the functions <code>mxSetPr</code>, etc., that perform the inverse, and set the data of an Array to use the block of memory pointed to by the argument of <code>mxSetPr</code>. <p>Note the type <code>mwSize</code> used above, and <code>mwIndex</code> are defined as the native precision of the indexing in Octave on the platform on which the mex-file is built. This allows both 32- and 64-bit platforms to support mex-files. <code>mwSize</code> is used to define array dimension and maximum number or elements, while <code>mwIndex</code> is used to define indexing into arrays. <p>An example that demonstration how to work with arbitrary real or complex double precision arrays is given by the file <samp><span class="file">mypow2.c</span></samp> as given below. <pre class="example"><pre class="verbatim"> #include "mex.h" void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { mwIndex i; mwSize n; double *vri, *vro; if (nrhs != 1 || ! mxIsNumeric (prhs[0])) mexErrMsgTxt ("expects matrix"); n = mxGetNumberOfElements (prhs[0]); plhs[0] = (mxArray *) mxCreateNumericArray (mxGetNumberOfDimensions (prhs[0]), mxGetDimensions (prhs[0]), mxGetClassID (prhs[0]), mxIsComplex (prhs[0])); vri = mxGetPr (prhs[0]); vro = mxGetPr (plhs[0]); if (mxIsComplex (prhs[0])) { double *vii, *vio; vii = mxGetPi (prhs[0]); vio = mxGetPi (plhs[0]); for (i = 0; i < n; i++) { vro [i] = vri [i] * vri [i] - vii [i] * vii [i]; vio [i] = 2 * vri [i] * vii [i]; } } else { for (i = 0; i < n; i++) vro [i] = vri [i] * vri [i]; } } </pre></pre> <p class="noindent">with an example of its use <pre class="example"> b = randn(4,1) + 1i * randn(4,1); all(b.^2 == mypow2(b)) ⇒ 1 </pre> <p>The example above uses the functions <code>mxGetDimensions</code>, <code>mxGetNumberOfElements</code>, and <code>mxGetNumberOfDimensions</code> to work with the dimensions of multi-dimensional arrays. The functions <code>mxGetM</code>, and <code>mxGetN</code> are also available to find the number of rows and columns in a matrix. </body></html>