Alternative to eval('equation in string form')
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to know how to avoid eval to solve an equation or expression that is in string form . For eg.if the equation is 'abs(Array1) + min(Single value1) .* log(Array2)' ie in string format . Using eval('equation in string form') does work most of the times . Except when the sizes of abs(Array1) & log(Array2) are different . So is there any way to divide the equation , solve them one by one, check their sizes (interpolate if sizes are different) and then proceed further with the equation ?
This equation is just an example as it can have different forms/function even nestec functions ie functions within functions. Also though i am making the sizes of arrays like Array1,Array2 same before passing them to eval but sometimes the output from functions alters their size due to which further operations are not possible in eval.
댓글 수: 3
Guillaume
2017년 4월 5일
In my opinion, you have two options: use the parser you already have, matlab, which you invoke as you have done with eval. However, you have to abide by the rules of this parser, so the size of whatever you combine must be the same.
If you want to have your own rules, then your second option is to indeed write your own parser, which you can do either in .Net or matlab. Again, this is not trivial. Before you even start writing code, you need to define the whole grammar of your parser.
답변 (1개)
Steven Lord
2017년 4월 5일
Are you performing these computations symbolically?
x = randi(10, [1 5]);
syms Array1 Array2
y = abs(Array1)+min(x).*log(Array2);
If not, can you show a small example of source code that you're currently using to "solve an equation or expression that is in string form"?
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!