我編寫了一個C++函數及其關聯的mex。但是C++函數的一種輸入是double *
。錯誤無法從'void *'轉換爲'float *'
函數
pointwise_search
的輸出是一個指針。我被告知我應該刪除它。但我不知道我應該刪除它,因爲我需要它作爲輸出。從答案中,我知道我應該檢查
mxIsSingle
的輸入類型。所以我糾正了功能mexFunction
。但是有一個錯誤error C2440: '=' : cannot convert from 'void *' to 'float *'
。在Matlab中,我應該叫喜歡
pointwise_search(
浮動* P,
浮動q, num_thres,
浮動ñ, len)
。如果我在matlab中有矢量v_in_matlab = rand(5,1)。我我應該得到它的p=single(v_in_matlab);
指針,提前然後pointwise_search(p...
感謝。
#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);
}
感謝您的評論。你能告訴我應該在哪裏刪除函數'pointwise_search'的輸出'Y' – Vivian