Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用Matlab mex输出二维整数数组?_C++_Arrays_Matlab_Mex - Fatal编程技术网

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

C++ 如何使用Matlab mex输出二维整数数组?,c++,arrays,matlab,mex,C++,Arrays,Matlab,Mex,我得到了一个关于MatlabMex函数输入/输出2D数组格式的后续问题。例如,我有一个变量“ OutPuffF”,定义为C++ 2D整数数组。在处理它之后,我想将它输出为“plhs”(左侧的参数)。不知道怎么做 int** outputBuff; size_t col_outputBuff = mxGetN(prhs[4]); size_t row_outputBuff = mxGetM(prhs[4]); // Allocate the memory for 2D

我得到了一个关于MatlabMex函数输入/输出2D数组格式的后续问题。例如,我有一个变量“<代码> OutPuffF”,定义为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];
    }
}
代码可以编译,但输出的所有零都不是预期的。你能给我一些提示吗?谢谢。A

编辑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];
        }
    }

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

代码的问题在于您没有清楚地理解赋值对变量的作用。如果分配
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];
     }
}
//创建输出数组,包括内存缓冲区
plhs[0]=mxCreateNumericMatrix(列输出buff、行输出buff、mxINT32类、mxREAL);
//获取指向存储数据的内存的指针
int*outputBuffer=(int*)mxGetData(plhs[0]);
int*inputBuffer=(int*)mxGetData(prhs[4]);
for(int col=0;col
就这样。请注意,我正在索引输出矩阵,就像索引输入一样:作为一个连续的内存缓冲区,使用乘法


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

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

// 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];
    }
}
//输出结果
plhs[0]=mxCreateNumericMatrix(列输出buff、行输出buff、mxINT32类、mxREAL);
int*输出矩阵=(int*)mxGetData(plhs[0]);
//读入数据
for(int col=0;col
您更改了我访问outputBuffer的代码。不要。MATLAB数组是单个内存缓冲区。因为你需要一个2D矩阵,然后像你在第一个例子中做的那样,用mxCalloc分配一个(称为不同的东西),处理它,然后在最后一步,用我的代码将它逐元素复制到outputBuffer中。嗨,Peter,我用我的Edit2再次尝试,但仍然不起作用。请帮我检查一下。非常感谢。准确地说,我认为在这之后你需要在matlab中转置结果。首先更改索引,就像您在matlab转置中所做的(cols,rows)和后面的一样。
int** outputMatrix = (int**)mxGetData(plhs[0]);
// 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];
     }
}
// 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];
    }
}