Mex results different from linux to windows!

조회 수: 5 (최근 30일)
Alessandro Masullo
Alessandro Masullo 2015년 2월 12일
답변: Alessandro Masullo 2015년 2월 13일
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