gpt4 book ai didi

c++ - mexfunction 输入困惑

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:26:16 25 4
gpt4 key购买 nike

我是 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 %f\n", in_A[2], in_A[1]);
//mexPrintf("%d\n", 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 中运行脚本时,我遇到了问题。它还可以正确读取 nrowsncols 变量。问题是我无法在函数中插入正确的输入。我试图打印一些 vector 元素,但它打印的元素与输入 vector 元素不同。我这样调用 mexfunction a = addVector([1.1 2.2 355],[2.5 45 5.5])。谁能赐教一下??

提前谢谢你。

PS:Matlab 2011a、VS 2010、Win 7 64x。

最佳答案

让我举例说明我将如何实现这样的功能:

添加 vector .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 数组(但可以是任何维度,包括标量、 vector 、矩阵、N 维数组)。

>> 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

关于c++ - mexfunction 输入困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com