【问题标题】:Changing the constant arguments in mex-function更改 mex 函数中的常量参数
【发布时间】:2016-07-30 00:54:17
【问题描述】:

这个问题是我正在进行的一个更大项目的一部分。我使用了一个更简单的 mex 函数来解释我正在处理的问题。

要求是更改传递给 mex 函数的参数(RHS 上的变量)。这是必要的要求。 在双 * 作为 argumjents 的情况下,我已经能够更改变量。代码如下:

#include "mex.h"
/* The computational routine */
void arrayProduct(double x, double *y, double *z, mwSize n)
    {
    mwSize i;
    /* multiply each element y by x */
    for (i=0; i<n; i++) {
        z[i] = (x * y[i]);
    }
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
              int nrhs, const mxArray *prhs[])
{
double multiplier;              /* input scalar */
double *inMatrix;               /* 1xN input matrix */
size_t ncols;                   /* size of matrix */
double *outMatrix;              /* output matrix */

/* check for proper number of arguments */
if(nrhs!=3) {
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Three inputs required.");
}
if(nlhs!=0) {
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","Zero output required.");
}

/* get the value of the scalar input  */
multiplier = mxGetScalar(prhs[0]);
/* create a pointer to the real data in the input matrix  */
inMatrix = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[1]);

/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(prhs[2]);
/* call the computational routine */
arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);

}

当我尝试使用类型转换为 int * 来做同样的事情时,它不起作用。 这是我尝试过的代码:

包括“mex.h”

/* The computational routine */
void arrayProduct(double x, double *y, int *z, mwSize n)
{
    mwSize i;
    /* multiply each element y by x */
    for (i=0; i<n; i++) {
        z[i] = (x * y[i]);
    }
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
    double multiplier;              /* input scalar */
    double *inMatrix;               /* 1xN input matrix */
    size_t ncols;                   /* size of matrix */
    int *outMatrix;              /* output matrix */
    /* check for proper number of arguments */
    if(nrhs!=3) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Two inputs required.");
    }
    if(nlhs!=0) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
    }

    /* get the value of the scalar input  */
    multiplier = mxGetScalar(prhs[0]);
    int mult = (int)multiplier;
    /* create a pointer to the real data in the input matrix  */
    inMatrix = mxGetPr(prhs[1]);
   /* int *inMat;
    inMat = *inMatrix;*/
    /* get dimensions of the input matrix */
    ncols = mxGetN(prhs[1]);
    /* create the output matrix */
    /* get a pointer to the real data in the output matrix */
    outMatrix = (int *)mxGetData(prhs[2]);
    /* call the computational routine */
    arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}

对于我的项目,我需要将 double 转换为 int * 并在这个简单的示例中解决它可以解决问题。 有什么建议吗?

【问题讨论】:

  • 注意: 明确禁止更改 MEX 文件中的 RHS 参数。由于 MATLAB 的延迟复制,您也可能会导致其他矩阵发生意外更改。声明这些数组是有原因的 const!

标签: c++ c matlab mex


【解决方案1】:

将指针转换为其他类型不会将其指向的数据转换为该类型。几乎所有的 Matlab 数据都是double 的数组。如果您的函数需要 int 的数组,您需要为 ints 分配一个单独的数组并一次转换一个元素。

【讨论】:

  • 是的,但它违背了使用 mex 函数来提高速度的通常目的..
猜你喜欢
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
相关资源
最近更新 更多