mex.cpp例子

一、arrayProduct.cpp

# include "mex.h"
#include<iostream>
using namespace std;
void arrayProduct(double x, double *y, double *z, int n)
{
	for (int i = 0; i < n; i++)
	{
		*(z + i) = *(y + i) * x;
		cout << *(z + i) << " " << endl;
	}
}

/* 入口函数 */
void mexFunction(int nlhs, mxArray *plhs[],
	int nrhs, const mxArray *prhs[])
{
	double multiplier;              /* 输入标量x*/
	double *inMatrix;               /* 输入1×n的矢量y*/
	size_t ncols;                   /* 输入矢量的维度*/
	double *outMatrix;              /* 输出矩阵 */

	/* 以下检查函数参数个数是否正确,可以省略*/
	if (nrhs != 2) {
		mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs", "Two inputs required.");
	}
	if (nlhs != 1) {
		mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs", "One output required.");
	}
	/* make sure the first input argument is scalar */
	if (!mxIsDouble(prhs[0]) ||
		mxIsComplex(prhs[0]) ||
		mxGetNumberOfElements(prhs[0]) != 1) {
		mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar", "Input multiplier must be a scalar.");
	}

	/* make sure the second input argument is type double */
	if (!mxIsDouble(prhs[1]) ||
		mxIsComplex(prhs[1])) {
		mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble", "Input matrix must be type double.");
	}

	/* check that number of rows in second input argument is 1 */
	if (mxGetM(prhs[1]) != 1) {
		mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector", "Input must be a row vector.");
	}

	/* 获得x的值 */
	multiplier = mxGetScalar(prhs[0]);

	/* 创建一个指向输入矢量y的指针 */
	inMatrix = mxGetPr(prhs[1]);

	/* 获得输入矢量y的维度n */
	ncols = mxGetN(prhs[1]);

	/* 创建一个输出矩阵*/
	plhs[0] = mxCreateDoubleMatrix(1, (mwSize)ncols, mxREAL);

	/* 获得指向输出矢量z的指针*/
	outMatrix = mxGetPr(plhs[0]);

	/* 调用计算程序 */
	arrayProduct(multiplier, inMatrix, outMatrix, (mwSize)ncols);
}

Matlab调用:

x=5;
A=[1,2,3,4];
B=arrayProduct(5,A);

二、hilb.cpp

# include "mex.h"

void hilb(double *y, int n)
{
	int i, j;
	for (i = 0; i<n; i++)
	for (j = 0; j<n; j++)
		*(y + j + i*n) = 1 / ((double)i + (double)j + 1);
}


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

	//x is the input param,y is the pointer to the matrix create in c
	double x, *y;
	//the size of the matrix
	int n;

	//get the size of matrix we want  to create
	x = mxGetScalar(prhs[0]);

	//create a x*x matrix,the element's datatype is real double,

	//and the matrix's pointer is associated with the firt output param.
	plhs[0] = mxCreateDoubleMatrix(x, x, mxREAL);


	//get the row numbers of the created matrix
	n = mxGetM(plhs[0]);

	//get the pointer of the created matrix
	y = mxGetPr(plhs[0]);

	//call subfunction
	hilb(y, n);
}

Matlab调用:a=hilb(5)

三、ResizeArray.cpp

#include "mex.h" 

//MATLAB调用形式: [resizedArr, resizedDims] = ResizeArray(arr, selRows, sekCols)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	if (nrhs != 3)
	{
		mexErrMsgTxt("参数个数不正确!");
	}

	int rowNum = mxGetM(prhs[0]);
	int colNum = mxGetN(prhs[0]);
	double* pArr = (double*)mxGetPr(prhs[0]);
	//得到选择的行列信息
	//无论是行向量还是列向量均支持
	double* pSelRows = (double*)mxGetPr(prhs[1]);
	double* pSelCols = (double*)mxGetPr(prhs[2]);
	
	int selRowsRowNum = mxGetM(prhs[1]);
	int selRowsColNum = mxGetN(prhs[1]);
	if (selRowsRowNum != 1 && selRowsColNum != 1)
	{
		mexErrMsgTxt("行参数不正确!");
	}
	int selRowsNum = selRowsRowNum*selRowsColNum;

	int selColsRowNum = mxGetM(prhs[2]);
	int selColsColNum = mxGetN(prhs[2]);

	if (selColsRowNum != 1 && selColsColNum != 1)
	{
		mexErrMsgTxt("列参数不正确!");
	}
	int selColsNum = selColsRowNum*selColsColNum;

	plhs[1] = mxCreateDoubleMatrix(2, 1, mxREAL);
	double* resizedDims = (double*)mxGetPr(plhs[1]);
	resizedDims[0] = selRowsNum;
	resizedDims[1] = selColsNum;

	plhs[0] = mxCreateDoubleMatrix(selRowsNum, selColsNum, mxREAL);
	double* pResizedArr = (double*)mxGetPr(plhs[0]);

	//这里因为MATLAB中数据得按列优先
#define ARR(row,col) pArr[(col)*rowNum+row]
#define RARR(row,col) pResizedArr[(col)*selRowsNum+row]
	for (int ri = 0; ri<selRowsNum; ri++)
	{
		for (int ci = 0; ci<selColsNum; ci++)
		{
			RARR(ri, ci) = ARR((int)pSelRows[ri] - 1, (int)pSelCols[ci] - 1);
		}
	}
	mexPrintf("OK! \n");
}

Matlab调用:

arr=[11:19;21:29;31:39;41:49;51:59;61:69];
selRows=[1 3];
selCols=[2:4 5 9];
[rarr,rdims]=test1(arr,selRows,selCols);

其中:
arr为:
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
rarr为:
12 13 14 15 19
32 33 34 35 39
rdims为:
2
5

四、ArithmeticOp.cpp

/* ArithmeticOp.cpp */
#include "mex.h" //一定要包含这个头文件,这个头文件是MATLAB为MEX编译专门提供的头文件 
#include <cstdio>
 
// 被封装的函数声明
// Arithmetic_Operation.cpp
// 本函数实现了两个double型参数输入的四则运算,并保存在res数组中,顺序为加减乘除
 
#include <cstdtio>
void Arithmetic_Operation( double* res, double a, double b )
{
     res[0] = a + b;     
     res[1] = a - b;
     res[2] = a * b;
     res[3] = ( b == 0 ? 0 : a / b ) ; // check whether divide is zero
     
     return;
}

 
/*
* 请注意下面的这个mexFunction函数,如果需要使用mex编译C++函数,一定要使用这个函数
* mexFunction就像#include "mex.h"一样是必须的,相当于是MATLAB和C++的一个接口函数
* 这个函数的四个参数都是有用的,它们分别代表的意思是:
* nlhs: 输入参数个数(MATLAB调用语句中,等号左边的参数个数)  plhs: 输入参数列表
* nrhs: 输出参数个数(MATLAB调用语句中,等号右边的参数个数) prhs: 输处参数列表
*/
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[] )
{
     if (nlhs != 1 || nrhs != 2 )// 判断调用参数数量是否正确
     {
          printf("Invalid Argument Usage!\n");
          exit(0);
     }
     
     // get input data
     double* a = mxGetPr(prhs[0]); // 输入参数列表,下标0是第一个输入参数
     double* b = mxGetPr(prhs[1]); // 类似于C++ main函数的两个参数(int argc, char** argv)的使用
     double* result = (double *)malloc(4*sizeof(double));
     
     // invoke 
     Arithmetic_Operation(result, *a, *b);
     
     // output data
     plhs[0] = mxCreateDoubleMatrix(4, 1, mxREAL);// 用于创建一个double型的矩阵,三个参数分别是矩阵的行,列,类型
     double* output = (double *)mxGetPr(plhs[0]); // mexGetPr()函数是mex.h中提供的用于获取输入输出参数地址的函数
 
     for (int i = 0; i < 4; i++)
          output[i] = result[i];
 
     free(result);
     return;
}
 
/* 被封装的函数 */
void Arithmetic_Operation( double* res, double a, double b )
{
     res[0] = a + b;     
     res[1] = a - b;
     res[2] = a * b;
     res[3] = ( b == 0 ? 0 : a / b ) ; // check whether divide is zero
     
     return;
}
// end of implementation

Matlab调用:[ res ] = ArithmeticOp(a, b);

参考:
https://www.cnblogs.com/ynnie/p/9239347.html
https://blog.csdn.net/codersadis/article/details/48266905
https://blog.csdn.net/john_bian/article/details/79067765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值