converting to mex

2 views (last 30 days)
Joshua
Joshua on 26 Dec 2011
Hi,
I am trying to write a simple mex code which extracts a subvector from a vector. Does anyone know the mex formatting (or has a simple example) when using vector<double> X.
I can't seem to get the mexFunction working properly with vectors.
for reference my c code is as follows:
void Vecs(vector<double> iv)
{
unsigned int i;
int min = 5;
int max = 20;
// Remove elements that have values less than the min
// cout << "Removing values less than " << min << endl;
for ( i=0; i<iv.size(); i++ ) {
if ( iv[i] < min ) {
iv.erase( iv.begin()+i );
i--;
}
}
// Remove elements that have values greater than the max
// cout << "Removing values less than " << max << endl;
for ( i=0; i<iv.size(); i++ ) {
if ( iv[i] > max ) {
iv.erase( iv.begin()+i );
i--;
}
}
return;
}
thanks
joshua
  2 Comments
Walter Roberson
Walter Roberson on 26 Dec 2011
That is not C code. C does not permit "<" or ">" to appear in a position where a type is expected (except that the operators could appear in constant arithmetic expressions used to construct a size.)
C also does not use "cout <<", but you have commented those out so that should only be considered a strong hint rather than a definite indication that the code is not C.
Is this routine to be called from a C++ routine, or is it to be called from MATLAB? If it is to be called from MATLAB, then MATLAB is not able to deal with vector<double> data type.
James Tursa
James Tursa on 26 Dec 2011
@Joshua: This is cross-posted in the newsgroup. Where do you want the discussion to take place? Here or in the newsgroup?

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 27 Dec 2011
Repeating my response from the newsgroup here just to complete this thread:
Try to access the data only once (i.e., drag it through the high level cache only once) and copy it only once. E.g., here is a simple mex routine that accomplishes what I think you are trying to do (change the >= and <= to suit your actual needs):
// File: gele.c
// B = gele(A,minval,maxval)
// returns the same result as the MATLAB m-code:
// B = A( A>=minval & A<=maxval )
// Single pass through A, wasted memory on tail of B
// This is fast, but B will have wasted memory at the tail since it will
// take the same amount of physical memory as A even if it is smaller.
// CAUTION: Bare bones code, no argument checking.
// Programmer: James Tursa
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mwSize i, n;
double minval, maxval;
double *Apr, *Bpr, *Bpr0;
n = mxGetNumberOfElements(prhs[0]);
minval = mxGetScalar(prhs[1]);
maxval = mxGetScalar(prhs[2]);
plhs[0] = mxCreateDoubleMatrix(1,n,mxREAL);
Apr = mxGetPr(prhs[0]);
Bpr = mxGetPr(plhs[0]);
Bpr0 = Bpr;
for( i=0; i<n; i++ ) {
if( *Apr >= minval && *Apr <= maxval ) {
*Bpr++ = *Apr;
}
Apr++;
}
mxSetN(plhs[0],Bpr-Bpr0); // Does *not* free up the trailing memory!
}
The above is just one way to do it that attempts to optimize the result for speed at the expense of memory. Basically it allocates a variable that is the same size as the input and then fills in the result in a single pass through both variables. The downside is that it retains the entire memory block even if the resulting dimensions are small. If you don't like this, then one could either count the result size ahead of time and then allocate only that much memory for the result up front (would require two passes through the input A), or perhaps reallocate the result to a smaller memory block once the result size is known (requires two passes through the result B), or some variation of either of these.
  2 Comments
Jan
Jan on 27 Dec 2011
Thanks, James, for caring about the readers of this cross-posted question.
An mxRealloc would be efficient, because it usually does not copy the data.
James Tursa
James Tursa on 27 Dec 2011
FYI, mxRealloc has a bug in later versions of MATLAB on Windows and does *not* free up any trailing memory when going from a very large block to a very small block. So unfortunately one can't rely on it for this purpose.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!