2016-07-30 24 views
1

Diese Frage ist Teil eines größeren Projekts, an dem ich gerade arbeite. Ich habe eine einfachere mex-Funktion verwendet, um das Problem zu erklären, mit dem ich es zu tun habe.Ändern der Konstantenargumente in der Mex-Funktion

Die Anforderung besteht darin, die an die mex-Funktion übergebenen Argumente (Variablen auf der RHS) zu ändern. Dies ist eine notwendige Voraussetzung. Ich habe die Variable im Falle von double * als argumjents ändern können. Hier ist der Code:

#include "mex.h" 
/* The computational routine */ 
void arrayProduct(double x, double *y, double *z, mwSize n) 
    { 
    mwSize i; 
    /* multiply each element y by x */ 
    for (i=0; i<n; i++) { 
     z[i] = (x * y[i]); 
    } 
} 

/* The gateway function */ 
void mexFunction(int nlhs, mxArray *plhs[], 
       int nrhs, const mxArray *prhs[]) 
{ 
double multiplier;    /* input scalar */ 
double *inMatrix;    /* 1xN input matrix */ 
size_t ncols;     /* size of matrix */ 
double *outMatrix;    /* output matrix */ 

/* check for proper number of arguments */ 
if(nrhs!=3) { 
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Three inputs required."); 
} 
if(nlhs!=0) { 
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","Zero output required."); 
} 

/* get the value of the scalar input */ 
multiplier = mxGetScalar(prhs[0]); 
/* create a pointer to the real data in the input matrix */ 
inMatrix = mxGetPr(prhs[1]); 
/* get dimensions of the input matrix */ 
ncols = mxGetN(prhs[1]); 

/* get a pointer to the real data in the output matrix */ 
outMatrix = mxGetPr(prhs[2]); 
/* call the computational routine */ 
arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols); 

} 

Wenn ich versuche, das gleiche zu tun mit typecasting zu int * es funktioniert nicht. Hier ist der Code, den ich versucht habe:

umfassen „mex.h“

/* The computational routine */ 
void arrayProduct(double x, double *y, int *z, mwSize n) 
{ 
    mwSize i; 
    /* multiply each element y by x */ 
    for (i=0; i<n; i++) { 
     z[i] = (x * y[i]); 
    } 
} 

/* The gateway function */ 
void mexFunction(int nlhs, mxArray *plhs[], 
        int nrhs, const mxArray *prhs[]) 
{ 
    double multiplier;    /* input scalar */ 
    double *inMatrix;    /* 1xN input matrix */ 
    size_t ncols;     /* size of matrix */ 
    int *outMatrix;    /* output matrix */ 
    /* check for proper number of arguments */ 
    if(nrhs!=3) { 
     mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Two inputs required."); 
    } 
    if(nlhs!=0) { 
     mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required."); 
    } 

    /* get the value of the scalar input */ 
    multiplier = mxGetScalar(prhs[0]); 
    int mult = (int)multiplier; 
    /* create a pointer to the real data in the input matrix */ 
    inMatrix = mxGetPr(prhs[1]); 
    /* int *inMat; 
    inMat = *inMatrix;*/ 
    /* get dimensions of the input matrix */ 
    ncols = mxGetN(prhs[1]); 
    /* create the output matrix */ 
    /* get a pointer to the real data in the output matrix */ 
    outMatrix = (int *)mxGetData(prhs[2]); 
    /* call the computational routine */ 
    arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols); 
} 

Ich brauche doppelt zu konvertieren es auf diesem einfachen Beispiel in int * im Falle von meinem Projekt und Lösung löst die Problem. Irgendwelche Vorschläge?

Antwort

1

Wenn Sie einen Zeiger auf einen anderen Typ konvertieren, werden die Daten, auf die er verweist, nicht in diesen Typ konvertiert. Fast alle Matlab-Daten sind Arrays von double. Wenn Ihre Funktion ein Array von int erfordert, müssen Sie ein separates Array für die int s zuweisen und die Elemente einzeln konvertieren.

+0

Ja, aber es vereitelt den üblichen Zweck der Verwendung von mex-Funktionen zur Erhöhung der Geschwindigkeiten. – Pundit