Mex results different from linux to windows!

1 次查看(过去 30 天)
Hello everyone.
I have a mex routine written in C++ that I use on a Windows computer compiled through Visual Studio. Now I need the same routine running on a Linux computer and I'm using gcc to compile it. I always compile the routine with a simple mex filename.cpp, but the results that I get on Windows are completely different from those on the Linux machine.
How can this be possible?
This is the structure of the code:
#include "mex.h"
#include <matrix.h>
#include <vector>
#include <math.h>
#include <iostream>
#include <algorithm>
const double pi = 3.141592653589793;
double median(double *x, const int NN);
void mexFunction(int nlhs, mxArray *plhs[], /* Output variables */
int nrhs, const mxArray *prhs[]) /* Input variables */
{
double *xc = mxGetPr(prhs[0]);
// [Read variables]
// [Output]
plhs[0] = mxCreateDoubleMatrix(N_ws,1,mxREAL);
double *norm = mxGetPr(plhs[0]);
// [Declare dynamic variables]
double *var = new double[N];
// [Simple for cycle with calculations]
// [Delete dynamic variables]
delete[] var;
return;
}
double median(double *x, const int NN) {
double med;
// Even number of samples
if (NN % 2 == 0) {
double x1, x2;
std::nth_element(&x[0], &x[NN/2-1], &x[NN]); // I have to put x[NN] and not x[NN-1] because the last is not sorted!
x1 = x[NN/2-1];
std::nth_element(&x[0], &x[NN/2], &x[NN]); // I have to put x[NN] and not x[NN-1] because the last is not sorted!
x2 = x[NN/2];
med = (x2 + x1)/2;
// Odd number of samples
} else {
std::nth_element(&x[0], &x[0] + (NN-1)/2, &x[NN]);
med = x[(NN-1)/2];
}
return med;
}

采纳的回答

Alessandro Masullo
Alessandro Masullo 2015-2-13
I answer myself. The problem was related to the function abs. In gcc, math.h has a C version of abs that works on integers, so gcc abs was rounding all my values to integers!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by