Sort array based on particular rows

27 ビュー (過去 30 日間)
MechenG
MechenG 2025 年 2 月 12 日 16:52
編集済み: Stephen23 2025 年 2 月 12 日 18:50
Hi,
I have 33 x 2 array, here I need to re arrange the values in descending order based on values in the first colum. I know that I can use 'sort' function for this.
However here the challenge is I need to re arrange first 6 rows and next 5 rows, then next 6 rows and then next 5 rows like this until 33 rows. Could some one please help with this?
  1 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 12 日 17:30
It would be easiest to loop.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 2 月 12 日 18:14
編集済み: Stephen23 2025 年 2 月 12 日 18:50
format long G
M = load('matlab.mat').out
M = 33×2
1.0e+00 * 233.873510212435 257.536364606214 599.769141173988 259.501128995219 965.013054011521 262.319982477876 1329.49900438918 264.502023540547 1694.31692033012 266.546157209838 2060.474141887 268.70258090239 415.310320095342 575.027170072566 780.458968117364 577.922155410056 1145.03025727357 579.593500883109 1509.969785636 581.788952871061
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = -fix(2*(0:size(M,1)-1)/11);
[~,Y] = sortrows([X(:),M],'descend');
Z = M(Y,:)
Z = 33×2
1.0e+00 * 2060.474141887 268.70258090239 1694.31692033012 266.546157209838 1329.49900438918 264.502023540547 965.013054011521 262.319982477876 599.769141173988 259.501128995219 233.873510212435 257.536364606214 1875.00204476564 584.2432307386 1509.969785636 581.788952871061 1145.03025727357 579.593500883109 780.458968117364 577.922155410056
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
MechenG
MechenG 2025 年 2 月 12 日 18:44
Thanks!!

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2025 年 2 月 12 日 17:17
out = load('matlab.mat').out
out = 33×2
1.0e+03 * 0.2339 0.2575 0.5998 0.2595 0.9650 0.2623 1.3295 0.2645 1.6943 0.2665 2.0605 0.2687 0.4153 0.5750 0.7805 0.5779 1.1450 0.5796 1.5100 0.5818
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = mat2cell(out,[6 5 6 5 6 5]);
C = cellfun(@(x)sortrows(x,1,'descend'),C,'UniformOutput',false);
out = vertcat(C{:})
out = 33×2
1.0e+03 * 2.0605 0.2687 1.6943 0.2665 1.3295 0.2645 0.9650 0.2623 0.5998 0.2595 0.2339 0.2575 1.8750 0.5842 1.5100 0.5818 1.1450 0.5796 0.7805 0.5779
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
MechenG
MechenG 2025 年 2 月 12 日 18:43
Thanks!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by