How to sort numbers in a char

조회 수: 3 (최근 30일)
arthur doroshev
arthur doroshev 2020년 11월 30일
댓글: arthur doroshev 2020년 11월 30일
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
Rik
Rik 2020년 11월 30일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 11월 30일
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
arthur doroshev
arthur doroshev 2020년 11월 30일
Thank you very much Stephen Cobeldick

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by