MEX:如何从C++/C向MATLAB返回矩阵
MEX: How to return a matrix from C++/ C to MATLAB
我可以返回的矩阵只有第一个元素是正确的,其他元素都是零。我试着按照这个链接中的建议去做;但是,这对我来说并没有什么好处。如果能给我一些帮助,我将不胜感激。
输出:
>> Matlab_mex_Testing
B1 =
1.0000 0 0
0 1.0000 0
0 0 1.0000
0 0 0
B =
1 0 0
0 0 0
0 0 0
0 0 0
Matlab代码:
Dir2 = '/home/dkumar/Mex_Codes_DKU/MexCode_Working/Mex_CPP_N_ARMADILLO_Codes_DKU_makefile_Working';
% MEX
cd(Dir2);
[y, B] = normpdfDKU(1/2,0,1);
%Matlab declared
B1 = eye(4,3)
% Identical matrix returned from MEX
B
CPP:
#include "mex.h"
#include <math.h>
#include "/home/dkumar/armadillo-4.600.3/include/armadillo"
using namespace arma;
using namespace std;
#define pi (3.141592653589793)
extern void _main();
const int numInputArgs = 3;
const int numOutputArgs = 2;
// Function declarations.
// -----------------------------------------------------------------
double getMatlabScalar (const mxArray* ptr);
double& createMatlabScalar (mxArray*& ptr);
int TestingLibraries() ; // declared and defined in In Include_4_TSNNLS.c and Include_4_TSNNLS.h
// Function definitions.
// -----------------------------------------------------------------
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
// Check to see if we have the correct number of input and output
// arguments.
if (nrhs != numInputArgs)
mexErrMsgTxt(" DKU-1: Incorrect number of input arguments");
if (nlhs != numOutputArgs)
mexErrMsgTxt("DKU-2: Incorrect number of output arguments");
// Get the inputs.
double x = getMatlabScalar(prhs[0]);
double mu = getMatlabScalar(prhs[1]);
double v = getMatlabScalar(prhs[2]);
// Create the output. It is also a double-precision scalar.
double& p = createMatlabScalar(plhs[0]);
// Compute the value of the univariate Normal at x.
p = exp(-(x-mu)*(x-mu)/(2*v)) / sqrt(2*pi*v);
// CREATE ARMA::mat and print
mat B = eye<mat>(4,3);
//Print B
B.print();
// Trying to return B as second output
mwSize sz[2];
sz[0] = B.n_rows ; // Matlab is row first
sz[1] = B.n_cols ;
//mxArray* pOUT = mxCreateNumericArray(2, sz, mxDOUBLE_CLASS, mxREAL);
plhs[1] = mxCreateNumericArray(2, sz, mxDOUBLE_CLASS, mxREAL);
//Get a pointer to pOUT
double* p2 = (double*)mxGetData(plhs[1]);
*p2 = B[0];
}
double getMatlabScalar (const mxArray* ptr) {
// Make sure the input argument is a scalar in double-precision.
if (!mxIsDouble(ptr) || mxGetNumberOfElements(ptr) != 1)
mexErrMsgTxt("The input argument must be a double-precision scalar");
return *mxGetPr(ptr);
}
double& createMatlabScalar (mxArray*& ptr) {
ptr = mxCreateDoubleMatrix(1,1,mxREAL);
return *mxGetPr(ptr);
}
罪魁祸首就在这里:
*p2 = B[0];
这相当于只复制B
的第一个值,并将其放入p2
的第一个槽,即输出的第一行、第一列。您需要复制整个矩阵。
还要记住,MATLAB中的矩阵是列主矩阵,因此您需要了解对p2
进行索引的方式。然而,Armadillo指出,如果您只使用单个线性索引访问矩阵,则可以访问列主格式的元素。因此,与其执行*p2 = B[0];
,不如执行以下操作:
for (int i = 0; i < B.n_elem; i++)
p2[i] = B[i];
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- 如何发送 Mat H=findHomography 返回 Matlab
- 在MATLAB中仅接受INT32PTR时,将错误编号作为字符串返回
- WaitCommEvent 在启动时不会返回(当通过 MatLAB 初始化 COMPort 时有效)
- MEX:如何从C++/C向MATLAB返回矩阵
- 在c++中的一个函数中返回几个数组和变量(如MATLAB)
- MATLAB mex例程未返回正确的值
- 从c++ MEX文件返回值到MATLAB
- 犰狳求解(A,b)返回与Matlab,Eigen不同的答案