Operating on syms class matrices.
Show older comments
My algorithm returns matrices in the class "sym", of a similar form to the following matrix.
matrix =
[ a^2 + b*c, a*b + b*d]
[ a*c + c*d, d^2 + b*c]
I wish to switch the operators in this matrix. Such that, the '*' become '+' and vice versa. I then wish to use subs,
subs(mtx,[a b c d],[1 2 3 4])
in order to solve the matrix. Is there anyway to do this.
I can switch '*' for '+' and vice versa if I convert the matrices elements to char strings and simply search and replace, however this leaves solving the matrix for specific values of a,b,c and d problematic.
Any help would be greatly appreciated.
Regards
Ross
Accepted Answer
More Answers (1)
Walter Roberson
on 2 Dec 2011
What do you intend that a^2 + b*c would transform to?
- (a+a) * (b+c)
- a^2 * (b+c)
- (a^2 * b) + c
- a + (a*b) + c
What should a*b + b*d transform to?
- (a+b) * (b+d)
- a + (b*b) + d
- Or since a*b = b*a, the original expression could also be written b*a + d*b and does that perhaps transform to b + (a*d) + b ?
If you do textual substitutions you need to define the grouping of expressions.
For some of the grouping choices, you can handle the manipulation readily using subs(), such as
newmtx = subs(mtx, {sym('_plus'), sym('_mult')}, {sym('_mult'), sym('_plus')});
The _plus and _mult are the internal MuPAD names of + and * when used as operators: see http://www.mathworks.com/help/toolbox/mupad/stdlib/_mult.html
1 Comment
ross montgomery
on 5 Dec 2011
Categories
Find more on Utilities for the Solver in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!