- 积分
- 0
- 注册时间
- 2009-1-8
- 仿真币
-
- 最后登录
- 1970-1-1
|
最近初学混合编程,看董维国老师的《深入浅出Matlab混合编程》。
有一段C++写的Mex程序,编译通不过。找不出来原因,非常沮丧(说实话,我最怕就是把书上的码敲进去编译通不过了,感觉是自己犯了很低级的错误...)
大家帮忙看一下吧 谢谢了
#include "mex.h"
#include <vector>
#include <complex>
typedef std::vector<std::complex<double>> cmplxArray;
void mydiff(cmplxArray &z1, cmplxArray &z2, cmplxArray &out)
{
// 清除输出向量
out.clear();
// 计算两个输入向量的差
int i;
for (i=0; i<z1.size(); ++i)
{
out.push_back(z1-z2);
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i, m1, n1, m2, n2;
double *datar1, *datai1, *datar2, *datai2;
double *outr, *outi;
// 输入变量个数必须为2
if(nrhs != 2)
mexErrMsgTxt("The number of inputs must be two.");
// 输入必须是数字型
for(i=0; i<nrhs; ++i)
{
if(mxIsChar(prhs)||mxIsClass(prhs,"sparse"))
mexErrMsgTxt("Input must be real, full, and nonstring.");
}
// 得到每个输入矩阵的大小
m1 = mxGetM(prhs[0]);
n1 = mxGetN(prhs[0]);
m2 = mxGetM(prhs[1]);
n2 = mxGetN(prhs[1]);
// 输入矩阵大小必须相同
if(m1!=m2 || n1!=n2)
{
mexErrMsgTxt("The two inputs must be of the same size!");
}
// 创建输出变量所对应的mxArray
plhs[0] = mxCreateDoubleMatrix(m1,n1,mxCOMPLEX);
// 获得输入数据
datar1 = mxGetPr(prhs[0]);
datai1 = mxGetPi(prhs[0]);
datar2 = mxGetPr(prhs[1]);
datai2 = mxGetPi(prhs[1]);
cmplxArray z1, z2, out;
// 把数据存入向量中
for(i=0; i<m1*n1; ++i)
{
z1.push_back(std::complex<double>(datar1,datai1));
z2.push_back(std::complex<double>(datar2,datai2));
}
mydiff(z1, z2, out);
// 输出变量的数据所对应数组
outr = mxGetPr(plhs[0]);
outi = mxGetPi(plhs[0]);
// 把结果存入输出变量
for(i=0; i<m1*n1; ++i)
{
outr = out.real();
outi = out.imag();
}
} |
|