Borrar filtros
Borrar filtros

How to sort numbers in a char

4 visualizaciones (últimos 30 días)
arthur doroshev
arthur doroshev el 30 de Nov. de 2020
Comentada: arthur doroshev el 30 de Nov. de 2020
Hello,
I have a difficulty to make a sorting in a char. I need to sort the next char by the second number of each big Letter, for example:
G([1.1277],u1+[2.9793],[0.3143])+G([0.2594],u1+[3.8532],[0.31232])+Y([4.1689],u1+[9.0615],[0.024204])+D([1.2424],u1+[1.5934],[0.024204])
it should be like that as a result:
Y([4.1689],u1+[9.0615],[0.024204])+G([0.2594],u1+[3.8532],[0.31232])+G([1.1277],u1+[2.9793],[0.3143])+D([1.2424],u1+[1.5934],[0.024204])
If someone could help me with that, it will be very helpful
Thank a lot :)
  1 comentario
Rik
Rik el 30 de Nov. de 2020
You need to solve difficult problems step by step.
  1. Split the char into the terms
  2. Extract the relevant number from each term
  3. Use the second output of sort to determine the order
  4. Apply the sort to the tems
  5. Stich the terms back together

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 30 de Nov. de 2020
str = 'Y([4.1689],u1+[9.0615],[0.024204])+G([0.2594],u1+[3.8532],[0.31232])+G([1.1277],u1+[2.9793],[0.3143])+D([1.2424],u1+[1.5934],[0.024204])';
spl = regexp(str,'[A-Z]\([^\)]+\)','match');
vec = sscanf(str,'%*[A-Z]([%*f],u1+[%f],[%*f])+');
[~,idx] = sort(vec,'descend');
out = join(spl(idx),'+');
out = out{1}
out = 'Y([4.1689],u1+[9.0615],[0.024204])+G([0.2594],u1+[3.8532],[0.31232])+G([1.1277],u1+[2.9793],[0.3143])+D([1.2424],u1+[1.5934],[0.024204])'
  1 comentario
arthur doroshev
arthur doroshev el 30 de Nov. de 2020
Thank you very much Stephen Cobeldick

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by