c++ - 如何使用 Matlab mex 输出二维整数数组?

标签 c++ arrays matlab mex

我收到了有关 Matlab mex 函数输入/输出 2D 数组格式的后续问题。例如,我有一个变量“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]);

编译成功,但结果全为零,运气不好。请给你一张支票好吗?谢谢。

最佳答案

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

// 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];
    }
}

关于c++ - 如何使用 Matlab mex 输出二维整数数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21777549/

相关文章:

c++ - 绘制具有透明度的位图

matlab - 3-D 对象的多维数组 : how to vectorise inner products

matlab - 访问结构数据(matlab)

matlab - matlab中向量与 block 矩阵相乘

c++ - 有没有办法防止 header 定义的 c++ 函数被视为内联

c++ - libuvc 和 opencv2 的 G++ undefined reference

java - 如何使用java合并json数组?

c++ - 在运行时更改数组维度

c++ - 结构和数组的初始值设定项太多

c++ - 应用程序文件未在不同的 Mac 上运行