J'écris une fonction C++ et son mex associé. Mais le type d'entrée de la fonction C++ est double *
.Une erreur ne peut pas passer de 'void *' à 'float *' `
La sortie de la fonction
pointwise_search
est un pointeur. On m'a dit que je devrais le supprimer. Mais je ne sais pas où je devrais le supprimer car j'en ai besoin comme sortie.De la réponse, je sais que je devrais vérifier le type d'entrée par
mxIsSingle
. Donc, je corrige la fonctionmexFunction
. Mais il y a une erreurerror C2440: '=' : cannot convert from 'void *' to 'float *'
.Dans Matlab, je devrais appeler comme
pointwise_search(
flotteur * p,
flotteur q, num_thres,
flotteur n, len)
. Si j'ai un vecteur v_in_matlab = rand (5,1) dans matlab. Je devrais-je obtenir pointeur parp=single(v_in_matlab);
puispointwise_search(p...
Merci à l'avance.
#include "mex.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
float * pointwise_search(float *p,float *q,int num_thres, float* n, int len)
{
vector<float> P(p, p + num_thres);
vector<float> Q(q, q + num_thres);
int size_of_threshold = P.size();
float *Y=new float[len];
float *z=new float[len];
typedef vector<float > ::iterator IntVectorIt ;
IntVectorIt start, end, it, location ;
start = P.begin() ; // location of first
// element of Numbers
end = P.end() ; // one past the location
// last element of Numbers
for (int i=0;i<len;i++)
{
location=lower_bound(start, end, n[i]) ;
z[i]=location - start;
if(z[i]>0&&z[i]<size_of_threshold)
{
Y[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
}
else
{
Y[i]=Q[z[i]];
}
}
return (&Y[0]);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
float * Numbers, *Q;
if (nrhs != 5)
{
mexErrMsgTxt("Input is wrong!");
}
float *n = (float*) mxGetData(prhs[3]);
int len = (int) mxGetScalar(prhs[4]);
int num_thres = (int) mxGetScalar(prhs[2]);
/* Input gs */
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Numbers=mxGetData(prhs[0]);
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Q=mxGetData(prhs[1]);
// float * Numbers= (float *)mxGetData(prhs[0]);
// float * Q= (float *)mxGetData(prhs[1]);
float * out= pointwise_search(Numbers,Q,num_thres,n,len);
//float* resizedDims = (float*)mxGetPr(out);
}
merci pour votre commentaire. Pourriez-vous me dire où je devrais supprimer la sortie «Y» de la fonction 'pointwise_search' – Vivian