Operating on syms class matrices.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 2 de Dic. de 2011
variant
p1 = regexprep(char(A),'+','_')
p2 = regexprep(p1,'*','+')
out = sym(regexprep(p2,'_','*'))
Más respuestas (1)
Walter Roberson
el 2 de Dic. de 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
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!