Sort array based on particular rows

4 views (last 30 days)
MechenG
MechenG on 12 Feb 2025
Edited: Stephen23 on 12 Feb 2025
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?

Accepted Answer

Stephen23
Stephen23 on 12 Feb 2025
Edited: Stephen23 on 12 Feb 2025
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>

More Answers (1)

Voss
Voss on 12 Feb 2025
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>

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!