![]() |
我如何从Matlab重写方程式以在C ++中使用
我已经在Matlab中推导并简化了一个方程,并想在c ++程序中使用它。 Matlab喜欢使用幂, ^符号,但c ++一点也不喜欢。如何让Matlab重写方程式,以便输出c ++友好方程式?
[B]回答:[/B] 如果方程式的时间过长[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html"]而又[/URL]不想手工处理,则可以考虑重新格式化该方程式以使其对C ++友好的一种方法是使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html"]REGEXPREP[/URL]函数解析该方程式的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 。 [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexprep.html"]REGEXPREP[/URL]的输出将成为新的C ++友好方程newStr 。 您还可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#bqvj2yb-1"]动态运算符[/URL]更改替换字符串,以提供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 [url=https://stackoverflow.com/questions/2358049]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 04:55。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.