1

C++ で配列を作成または変更し、それを WarpControlPoints で配列として使用する方法の標準的な例があります。

/*==========================================================
 * arrayProduct.c - example in MATLAB External Interfaces
 *
 * Multiplies an input scalar (multiplier) 
 * times a 1xN matrix (inMatrix)
 * and outputs a 1xN matrix (outMatrix)
 *
 * The calling syntax is:
 *
 *      outMatrix = arrayProduct(multiplier, inMatrix)
 *
 * This is a MEX-file for MATLAB.
 * Copyright 2007-2012 The MathWorks, Inc.
 *
 *========================================================*/
/* $Revision: 1.1.10.4 $ */

#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 */

    /* 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]);

    /* create the output matrix */
    plhs[0] = mxCreateDoubleMatrix(1,(mwSize)ncols,mxREAL);

    /* get a pointer to the real data in the output matrix */
    outMatrix = mxGetPr(plhs[0]);

    /* call the computational routine */
    arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}

これは基本的に私が探しているもので、単純な配列ではなく 2D 配列を変更したいだけです。2D 配列 (4 xn) を作成し、4 行目を変更して動作するかどうかを確認しようとしました。次の行を変更すると:

/* 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[3][i] = x * y[i];
}
}

/* create the output matrix */
    plhs[0] = mxCreateDoubleMatrix(4,(mwSize)ncols,mxREAL);

うまくいきません。zフィールドでもポインターでもないというエラーが表示されます。私が間違ったことを誰か教えてもらえますか?

4

3 に答える 3

3

多次元配列は、2D C 配列ではなく、1 つの連続した配列として格納されます。データは列優先順です。つまり、z[0] は要素 (1,1) であり、z[1] は要素 (2,1) であり、z[4*N-1] まで続きます。

目的の 2D インデックス (行、列) (0 ベース) から線形インデックスを計算するには、 と書くだけidx = column*nrows + row;です。つまり、nrows 値を計算関数に渡す必要があります。

したがって、 という計算関数に追加のパラメーターを追加し、nrows呼び出すときにその値を渡します。上記のように、1D 配列としてインデックス z を付けます。

于 2013-08-29T16:09:34.203 に答える