Arithmetic to Functional Converter

조회 수: 1 (최근 30일)
Jonathan Sullivan
Jonathan Sullivan 2012년 3월 26일
I was wondering if anyone knows of a way to convert arithmetic to functional synatx. For example, I'd like to convert the string
'a+b'
to
'plus(a,b)'
Now, this was a very simple example. It gets a bit harder with more "complicated" expressions.
'a+b.*c'
becomes:
'plus(a,times(b,c))'
I assume MATLAB already does this, under the hood somewhere. There should be a way (either documented or not) to access this. Any insights?

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 26일
  댓글 수: 1
Jonathan Sullivan
Jonathan Sullivan 2012년 3월 27일
This looks helpful. I'm going to dig into it and see if I can hack it up to suit my needs.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

owr
owr 2012년 3월 27일
Are you just trying to evaluate algebraic statements provided in string format?
If so, try "eval":
>> a = 4
a =
4
>> b = 6
b =
6
>> str = 'a+b'
str =
a+b
>> eval(str)
ans =
10
  댓글 수: 1
Jonathan Sullivan
Jonathan Sullivan 2012년 3월 27일
No. What I am trying to do is to change current .m files so that they use bsxfun operation instead of the traditional operations. I've found myself on several occasions, painstakingly doing this by hand. It can be quite tedious and involve a lot of time troubleshooting, especially when the code has many nested algebraic functions. It is easy to put a parenthesis in the wrong spot.
Example:
(a+b.*c.^d).*x - y.*z.^x
Becomes:
bsxfun(@minus,bsxfun(@times,bsxfun(@plus,a,bsxfun(@times,b,bsxfun(@power,c,d))),x),bsxfun(@times,y,bsxfun(@power,z,x)))
I just thought it would be nice to have a function that will do it for me.

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2012년 3월 27일
Its easy, IF you look at this the right way.
First, pre-parse the expression to determine that 'a+b.*c' has three variables in it, a, b, and c. Define objects a, b and c. Now overload the plus and times operators to actually use bsxfun.
Finally, let MATLAB do the operation, using an eval. You CAN make this work, but you will need to be careful and think it out.
  댓글 수: 1
Jonathan Sullivan
Jonathan Sullivan 2012년 3월 27일
Yea. I'm aware that I can overload the functions and it'll work. But I'd like it to be stand alone. I tend to port my code over to may different people across many different machines. I would prefer to not have to define new objects and overload these functions on all their machines.

댓글을 달려면 로그인하십시오.


anisa
anisa 2012년 5월 31일
Hi, Did you manage to find a method in MATLAB that already implements this. I am trying to do the same thing I am sure that somewhere in matlab there should already be a method. If you could let me know that would be greatly appreciated.
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 5월 31일
I, on the other hand, am "sure" that there is no documented MATLAB method to do this (other than what John outlined.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by