mexfunction 输入混乱
mexfunction Inputs confusion
我是 mex 类型编程的新手,基本上我正在编写我的第一个 mexfunction,我遇到了一个愚蠢的问题。我附加了一部分代码:
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int mrows,ncols;
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
int numElements = ncols;
size_t size = numElements * sizeof(float);
float *in_A = (float *)mxMalloc(size);
float *in_B = (float *)malloc(size);
float *out_C = (float *)malloc(size);
if (in_A == NULL || in_B == NULL ||out_C == NULL)
{
mexErrMsgTxt("Failed to allocate host vectors!n");
}
if (nrhs != 2)
mexErrMsgTxt("Two inputs required. A vector and a multiplier");
if (nlhs != 1)
mexErrMsgTxt("One output required. The resulting vector");
in_A = (float *)mxGetPr(prhs[0]);
in_B = (float *)mxGetPr(prhs[1]);
//printf("%f %fn", in_A[2], in_A[1]);
//mexPrintf("%dn", prhs[1]);
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
out_C = (float *)mxGetPr(plhs[0]);
add_two_vectors(in_A,in_B,out_C);
}
函数add_two_vectors(float* A,float* B,float* C)
正在运行,但是当我在 mexfunction 中运行脚本时,我遇到了问题。此外,它还可以正确读取 nrows 和 ncols 变量。问题是我无法在函数中插入正确的输入。我尝试打印一些矢量元素,但它打印的元素与输入矢量元素不同。我像那样a = addVector([1.1 2.2 355],[2.5 45 5.5])
调用 mex函数.有人可以开导我吗?
提前谢谢你。
PS: Matlab 2011a, VS 2010, Win 7 64x.
让我举个例子,我将如何实现这样的函数:
add_vectors.cpp
#include "mex.h"
void add_two_vectors(float *c, const float *a, const float *b, const size_t sz)
{
for (size_t i=0; i<sz; i++) {
c[i] = a[i] + b[i];
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// input validation
if (nrhs != 2 || nlhs > 1) {
mexErrMsgTxt("Wrong number of input/output arguments.");
}
if (!mxIsSingle(prhs[0]) || !mxIsSingle(prhs[1])) {
mexErrMsgTxt("Inputs must be single arrays.");
}
if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1])) {
mexErrMsgTxt("Inputs must be real arrays.");
}
if (mxIsSparse(prhs[0]) || mxIsSparse(prhs[1])) {
mexErrMsgTxt("Inputs must be dense arrays.");
}
if (mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[1])) {
mexErrMsgTxt("Inputs must have the same size.");
}
// create ouput array
mwSize numel = mxGetNumberOfElements(prhs[0]);
mwSize ndims = mxGetNumberOfDimensions(prhs[0]);
const mwSize *dims = mxGetDimensions(prhs[0]);
plhs[0] = mxCreateNumericArray(ndims, dims, mxSINGLE_CLASS, mxREAL);
// get pointers to data
float *c = (float*) mxGetData(plhs[0]);
float *a = (float*) mxGetData(prhs[0]);
float *b = (float*) mxGetData(prhs[1]);
// perform addition: c = a + b
add_two_vectors(c, a, b, numel);
}
该函数需要两个大小相同的 single
类型的数组(但可以是任何维度,包括标量、向量、矩阵、N-D 数组)。
>> mex -largeArrayDims add_vectors.cpp
>> add_vectors(rand(4,'single'),rand(4,'single'))
ans =
0.6204 1.2307 1.1657 0.7583
1.3147 0.3817 0.3059 1.0377
0.8142 0.3519 0.8619 1.1154
1.0403 1.1578 0.1365 1.0048
您不能只将mxGetPr
(double*
)的输出静态转换为float*
而不会误解数据。 将内容读入双精度并转换每个元素,而不是指针。
或者,您可以在 MATLAB 中输入一个带有single(...)
的浮点数组,并使用 (float *)mxGetData
而不是 mxGetPr
。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 从输入文件到数组的混乱读取
- mexfunction 输入混乱
- 我想创建一个函数,我可以在其中输入一个字符串,它会输出一个混乱的字符串版本