Can I find and replace all multiplication operators in an expression with element-wise operators? I'd like to avoid manual entry!

1 view (last 30 days)
I am generating expressions that are functions of 2 unknowns, a and h. The expressions look something like this, but much longer:
S=(1366586424347476791*cot(a)*(h/(h + 1/10000) - 1))
I'd like to plug in arrays for the unknowns, and graph S versus h. However, I don't want to manually enter .*, ./, etc. for every operator (the expression is super long). Any tricks to replace all the operators in an expression with their element-wise equivalents?
Thanks for the help!

Answers (1)

Star Strider
Star Strider on 5 Feb 2016
You have to copy your expression as a string expression (easy enough — just put single quotes around it — inside the vectorize function), but vectorize will do it all for you:
% S=(1366586424347476791*cot(a)*(h/(h + 1/10000) - 1));
S = vectorize('(1366586424347476791*cot(a)*(h/(h + 1/10000) - 1))')
S =
(1366586424347476791.*cot(a).*(h./(h + 1./10000) - 1))
You have to edit it to put it on a single line and back into your code, but that’s easier than doing the vectorization yourself.
  2 Comments
Star Strider
Star Strider on 5 Feb 2016
My pleasure!
This is as close as you can get to automating it.
If my Answer came close enough to solving you problem, please Accept it!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!