Azzera filtri
Azzera filtri

How to sort numbers in a char

4 visualizzazioni (ultimi 30 giorni)
arthur doroshev
arthur doroshev il 30 Nov 2020
Commentato: arthur doroshev il 30 Nov 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 Commento
Rik
Rik il 30 Nov 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

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 30 Nov 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])'

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by