登录论坛

查看完整版本 : 我如何从Matlab重写方程式以在C ++中使用


poster
2019-12-10, 20:30
我已经在Matlab中推导并简化了一个方程,并想在c ++程序中使用它。 Matlab喜欢使用幂, ^符号,但c ++一点也不喜欢。如何让Matlab重写方程式,以便输出c ++友好方程式?



回答:

如果方程式的时间过长而又 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html)不想手工处理,则可以考虑重新格式化该方程式以使其对C ++友好的一种方法是使用REGEXPREP (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html)函数解析该方程式的MATLAB代码文本MATLAB。这是一个示例,说明如何用pow(x,2)或pow(y,3)替换形式x^2或y.^3表达式:

eqStr = 'the text of your equation code'; %# Put your equation in a string expr = '(\w+)\.?\^(\d+)'; %# The pattern to match repStr = 'pow($1,$2)'; %# The replacement string newStr = regexprep(eqStr,expr,repStr); %# The new equation string 您只需要获取MATLAB方程的代码,然后将其放在字符串变量eqStr 。 REGEXPREP (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html)的输出将成为新的C ++友好方程newStr 。

您还可以使用动态运算符 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#bqvj2yb-1)更改替换字符串,以提供x*x或y*y*y形式的结果。例如:

eqStr = 'the text of your equation code'; %# Put your equation in a string expr = '(\w+)\.?\^(\d+)'; %# The pattern to match repStr = '${repmat([$1,''*''],1,$2-''0''-1)}$1'; %# The replacement string newStr = regexprep(eqStr,expr,repStr); %# The new equation string

更多&回答... (https://stackoverflow.com/questions/2358049)