フィルターのクリア

How to sort numbers in a char

4 ビュー (過去 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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by