How to recognize multiplication with out *
Mostrar comentarios más antiguos
Hello!!
I want to write a code where you can input a string of a polynomial for instance '2x+3' and matlab returns the string as '2*x+3'. The user can input any string of a polynomial and matlab adds the * where needed.
I was thinking of possibly using a for loop (length of the string) where it passes through each indices and use isletter or isnumeric and if I get a numeric followed by a letter The code addes a * in between.
Can someone help me implement this or have a better way of doing so. Thanks a lot!
3 comentarios
James Tursa
el 19 de Mzo. de 2015
Editada: James Tursa
el 19 de Mzo. de 2015
Will there be expressions like 2/3x in the strings? If so, how should they be interpreted? As (2/3)*x or as 2/(3*x)? Will there be parentheses in the expressions?
John D'Errico
el 19 de Mzo. de 2015
And of course, how would it deal with the number 3e2, as this is a perfectly legal way of writing the number 300.
Dr. ARK
el 19 de Mzo. de 2015
Respuesta aceptada
Más respuestas (1)
Assuming you just want to insert a '*' between any number followed by any letter (that is ignore problems like brackets and 3e2 notation):
expression = '2x + 3 - 5y';
newexpression = regexprep(expression, '([0-9])([a-zA-Z])', '$1*$2')
edit: Actually, the following will not insert a '*' when the letter is followed by another number so will cope with 'e' notation:
expression = '2x + 3e5 - 1e2y';
newexpression = regexprep(expression, '([0-9])([a-zA-Z])(?![0-9])', '$1*$2')
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!