使用Matlab Coder将Matlab下的pinv函数转成C/C++

环境声明:Matlab R2013a

在这里,我将Matlab下的pinv函数转成了C代码

<1>编写一个函数

 

function b = funcpinv( a )%#codegen
%FUNCPINV Summary of this function goes here
%   Detailed explanation goes here
b = pinv(a);

end

<2>在命令窗口,输入mex -setpu,选中一个存在的编译器

<3>在命令窗口输入coder(图形界面),回车,弹出Coder Generation Project对话框

<4>在name中输入项目名 funcpinv.prj,点OK,弹出MATLAB Coder对话框

<5>点Entry-Point Files右下角的Add files,选择文件funcpinv.m打开

<6>点变量a右边的click to define,在这里我定义了double 3*3

<7>选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only

<8>在More settings的Speed中去掉support non-finite numbers,如果想生成C++代码,在All Settings中找到Advanced一栏,将其中的language选为C++

<9>点击Build,进行编译;点击View report,弹出Code Generation Report对话框,可以查看相应的代码

<10>在VS2005下编写测试代码:

 

// TestPinv.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
extern "C"
{
	#include "funcpinv.h"
};



int _tmain(int argc, _TCHAR* argv[])
{
	real_T matrix[9],matrix_pinv[9];
	int i = 0;
	for (i = 0;i < 9;i++)
	{
		matrix[i] = i + 1;
	}
	for (i = 0;i < 9;i++)
	{
		matrix_pinv[i] = 0;
	}
	for (i = 0;i < 9;)
	{
		std::cout<<matrix[i]<<"\t"<<matrix[i+1]<<"\t"<<matrix[i+2]<<std::endl;
		i += 3;
	}
	std::cout<<"start to pinv"<<std::endl;
	funcpinv(matrix,matrix_pinv);
	std::cout<<"pinv is finish"<<std::endl;
	std::cout<<"The matrix_pinv is"<<std::endl;
	for (i = 0;i < 9;)
	{
		std::cout<<matrix_pinv[i]<<"\t"<<matrix_pinv[i+1]<<"\t"<<matrix_pinv[i+2]<<std::endl;
		i += 3;
	}
	return 0;
}

注:Matlab build得到的是C代码,在编译时右键点击funcpinv.c,选择Properties,在对话框中选择Precompiled Headers,Create/use Precompiled Headers一栏选择Not Using Precompiled Headers

 

得到的结果:

参考:

1、录制的网上研讨会的MATLAB Coder视频

2、http://www.mathworks.cn/products/matlab-coder/

3、http://blog.csdn.net/fengbingchun/article/details/6793826

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值