Mex results different from linux to windows!

8 visualizzazioni (ultimi 30 giorni)
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;
}

Risposta accettata

Alessandro Masullo
Alessandro Masullo il 13 Feb 2015
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!

Più risposte (0)

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by