【问题标题】:How to output a 2D integer array using Matlab mex?如何使用 Matlab mex 输出二维整数数组?
【发布时间】:2014-02-14 11:00:17
【问题描述】:

我收到了一个关于 Matlab mex 函数输入/输出二维数组格式的后续问题。例如,我有一个变量 'outputBuff' 定义为 C++ 2D 整数数组。处理后,我想将其输出为“plhs”(左侧的参数)。不知道该怎么做。

int** outputBuff;

size_t col_outputBuff       = mxGetN(prhs[4]);
size_t row_outputBuff       = mxGetM(prhs[4]);

// Allocate the memory for 2D array
outputBuff                  = (int **)mxCalloc(col_outputBuff, sizeof(int*));

for(int x = 0; x < col_outputBuff; x++) {
    outputBuff[x] = (int *) mxCalloc(row_outputBuff, sizeof(int));
}

// Read in the data
for (int col=0; col < col_outputBuff; col++) {
    for (int row=0; row < row_outputBuff; row++) {
        outputBuff[col][row] = ((int *)mxGetPr(prhs[4]))[row+col*row_outputBuff];
    }
}

然后输出为plhs

const mwSize dims[]         = {col_outputBuff, row_outputBuff};
plhs[0]                     = mxCreateNumericArray(2, dims, mxINT32_CLASS, mxREAL);

mxArray *outputMatrix;
outputMatrix                = mxCreateNumericMatrix(col_outputBuff, row_outputBuff, mxINT32_CLASS, mxREAL);
outputBuff[0]               = (int *)mxGetPr(outputMatrix);
outputMatrix                = (mxArray *)mxGetData(plhs[0]);

可以编译代码,但输出的全零与预期不同。你能给我一些提示吗?非常感谢。答:

编辑 1:

嗨,彼得,感谢您的回复。我确实需要保留 C 样式的 2D 矩阵(或 2D 数组),因为我将 inputBuffer 定义为 int **。另外,我对inputBuffer做了一些处理,为了简化问题,我没有粘贴处理inputBuffer的代码。

以下内容不起作用:

int** inputBuffer;

// Codes to processing inputBuffer ... ...
// inputBuffer need to be C-Style 2D array

plhs[0]                     = mxCreateNumericMatrix(col_outputBuff, row_outputBuff, mxINT32_CLASS, mxREAL);
int** outputBuffer          = (int**)mxGetData(plhs[0]);

    for (int col=0; col < col_outputBuff; col++) {
        for (int row=0; row < row_outputBuff; row++) {
            outputBuffer[col][row]    = inputBuffer[col][row];
        }
    }

有什么想法吗?

编辑 2:

我已经按照你的提示再次尝试了:

int** outputBuff;

size_t col_outputBuff       = mxGetN(prhs[4]);
size_t row_outputBuff       = mxGetM(prhs[4]);

// Allocate the memory for 2D array
outputBuff                  = (int **)mxCalloc(col_outputBuff, sizeof(int*));

for(int x = 0; x < col_outputBuff; x++) {
    outputBuff[x] = (int *) mxCalloc(row_outputBuff, sizeof(int));
}

// Read in the data
for (int col=0; col < col_outputBuff; col++) {
    for (int row=0; row < row_outputBuff; row++) {
        outputBuff[col][row] = ((int *)mxGetPr(prhs[4]))[row+col*row_outputBuff];
    }
}

// Process the data save in outputBuff ...

// Create the output array, including memory buffers
plhs[0] = mxCreateNumericMatrix(col_outputBuff, row_outputBuff, mxINT32_CLASS, mxREAL);

// Get the pointer to the memory where you should store the data
int* outputMatrix = (int*)mxGetData(plhs[0]);

for (int col=0; col < col_outputBuff; col++) {
    for (int row=0; row < row_outputBuff; row++) {
        outputMatrix[row + col*row_outputBuff] = outputBuffer[row + col*row_outputBuff];
    }
}

但是,存在“无法将 int* 转换为 int **”的编译错误。然后我尝试投射

int** outputMatrix = (int**)mxGetData(plhs[0]);

得到编译但结果都是零,没有运气。请给我一张支票好吗?谢谢。

【问题讨论】:

    标签: c++ arrays matlab mex


    【解决方案1】:

    在彼得的帮助下,我得到了答案。我把它放在这里供其他人参考。

    // Output the results 
    plhs[0]                     = mxCreateNumericMatrix(col_outputBuff, row_outputBuff, mxINT32_CLASS, mxREAL);
    int* outputMatrix           = (int *)mxGetData(plhs[0]);
    
    // Read in the data
    for (int col=0; col < col_outputBuff; col++) {
        for (int row=0; row < row_outputBuff; row++) {
            outputMatrix[row + col*row_outputBuff] = outputBuff[col][row];
        }
    }
    

    【讨论】:

    • 确切地说,我认为您需要在此之后将结果转置到 matlab 中。首先更改索引,就像您所做的 (cols,rows) 并返回 matlab 转置。
    【解决方案2】:

    您的代码的问题在于您没有清楚地理解赋值对变量的作用。如果您分配outputMatrix = mxCreate...(),那么在两行outputMatrix = somethingelse 中,您已经覆盖了该值。 C 中的赋值将右边的值存储到左边的变量中。

    其实整个事情要简单得多:

    // Create the output array, including memory buffers
    plhs[0] = mxCreateNumericMatrix(col_outputBuff, row_outputBuff, mxINT32_CLASS, mxREAL);
    // Get the pointer to the memory where you should store the data
    int* outputBuffer = (int*)mxGetData(plhs[0]);
    int* inputBuffer  = (int*)mxGetData(prhs[4]);
    
    for (int col=0; col < col_outputBuff; col++) {
        for (int row=0; row < row_outputBuff; row++) {
            outputBuffer[row + col*row_outputBuff] = inputBuffer[row + col*row_outputBuff];
         }
    }
    

    就是这样。请注意,我正在像输入一样索引输出矩阵:作为连续的内存缓冲区,使用乘法。

    如果您的现有代码确实需要 C 样式的 2D 矩阵,请在最后一步以这种方式进行转换。

    【讨论】:

    • 您更改了我访问 outputBuffer 的代码。不。 MATLAB 数组是单个内存缓冲区。由于您需要一个 2D 矩阵,然后像在第一个示例中所做的那样使用 mxCalloc 分配一个(称为不同的东西),处理它,然后在最后一步,使用我的代码将其逐个元素复制到 outputBuffer 中。
    • 嗨,彼得,我再次尝试使用我的 Edit2,但仍然无法正常工作。请帮我检查一下。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多