Operating on syms class matrices.

1 visualización (últimos 30 días)
ross montgomery
ross montgomery el 2 de Dic. de 2011
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 2 de Dic. de 2011
variant
p1 = regexprep(char(A),'+','_')
p2 = regexprep(p1,'*','+')
out = sym(regexprep(p2,'_','*'))
  1 comentario
ross montgomery
ross montgomery el 5 de Dic. de 2011
Thanks Andrei.
This method worked well.
Regards
Ross

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
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
  1 comentario
ross montgomery
ross montgomery el 5 de Dic. de 2011
Hi Walter,
I should have defined this better. I want to keep the original order of the operations, i.e as would be performed in the original term.
I have managed to achieve this by using the "regexpre" function to substitute the operators and by introducing brackets in order to perform the operations in the order wanted. To do this is I simply wrap all '*' in brackets as such, ')*('. I then strcat brackets on to the start and the end of each matrix element in order to balance.
Thanks for your response Walter
Regards
Ross

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by