Clear Filters
Clear Filters

How to sort numbers in a char

4 views (last 30 days)
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 Comment
Rik
Rik on 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

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 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])'

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!