MATLAB:MEX矩阵除法得到的结果与m文件不同 [英] MATLAB: MEX matrix division gives different result than m-file

查看:218
本文介绍了MATLAB:MEX矩阵除法得到的结果与m文件不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MATLAB的编码器工具来创建矩阵指数函数的MEX版本,以用于另一组函数中.问题是,MEX版本提供的结果与原始m文件不同.

I've used MATLAB's coder tool to create a MEX version of the matrix exponential function, to be used in another set of functions. The issue is, the MEX version gives different results than the original m-file.

调试后,我相信这是因为MEX文件和m文件没有进行矩阵除法(\).否则,MEX文件首先会出现问题.直到发生矩阵除法的那一行的所有变量在两侧都是相等的.

After debugging it, I believe that the reason this is, is because the MEX file and the m-file do not do matrix division (\) the same. Or the MEX file has issues with it in the first place. All the variables leading up to the line where the matrix division occurs are equivalent on both sides.

这是发生问题的行:

F = (V-U)\(2*U) + I

其中我是V和U大小的恒等矩阵.

Where I is the identity matrix of the size of V and U.

当MEX文件进行矩阵分割时,差异背后的原因是什么?如何解决此问题?可以在不进行除法的情况下重写这行代码吗?

What is the reason behind the discrepancy when a MEX file does matrix division, and how can I fix this issue? Can this line of code be re-written without the division?

推荐答案

从这样的操作生成C代码没有问题.

I have no problem generating C code from such an operation.

这是我尝试过的测试功能:

Here is a test function I tried:

function F = myfcn(U,V)
    I = eye(size(U));
    F = (V-U)\(2*U) + I;
end

这是一个测试脚本,我们将使用它来验证结果:

here's a test script we'll use to verify the results:

U = rand(5);
V = rand(5);
F = myfcn(U,V);

我首先启动代码生成工具(ccoder),创建一个新项目集以生成MEX文件,然后从之前添加myfnc.m函数作为入口点.然后将两种输入变量类型定义为:

I start by launching the code generation tool (ccoder), create a new project set to produce a MEX-file, then add the myfnc.m function from before as entry-point. Then I define both input variables types as:

double (:Inf x :Inf)

指定一个无限制大小为double类型的MxN矩阵.

which specifies an MxN matrix of unbounded size of type double.

最后,我们可以构建项目.这样会产生myfcn_mex.mexw64.

Finally we can build the project. This produces myfcn_mex.mexw64.

测试原始的M函数和生成的MEX函数,我得到的结果几乎相同(差异按机器epsilon的顺序排列):

Testing both the original M-function and the generated MEX-function, I get pretty much the same result (difference is in the order of machine epsilon):

>> F = myfnc(U,V);
>> FF = myfcn_mex(U,V);
>> norm(F-FF)
ans =
   1.4391e-14

这篇关于MATLAB:MEX矩阵除法得到的结果与m文件不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆