Sort cell numbers ?

조회 수: 15 (최근 30일)
Heirleking
Heirleking 2022년 3월 22일
댓글: Image Analyst 2022년 3월 23일
I have a cell array that needs to be sorted both ascending and descending.
It is:
Groups = {[-83;-84] [-65] [-47;-50] [-30;-33] [-22;-26] [-15;-16] [-6; -7]}
Groups = 1×7 cell array
{2×1 double} {[-65]} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double}

채택된 답변

Voss
Voss 2022년 3월 23일
편집: Voss 2022년 3월 23일
"What I mean is how can I organize G in this way?"
G = {[-6; -7] [-16;-15] [-26;-22] [-33;-30] [-50;-47] [-65] [-83;-84]};
Maybe like this:
G = {[-83;-84] [-65] [-50;-47] [-33;-30] [-26;-22] [-16;-15] [-6; -7]};
[~,idx] = sort(cellfun(@(x)max(abs(x)),G));
G = G(idx);
format compact
celldisp(G);
G{1} = -6 -7 G{2} = -16 -15 G{3} = -26 -22 G{4} = -33 -30 G{5} = -50 -47 G{6} = -65 G{7} = -83 -84
  댓글 수: 1
Heirleking
Heirleking 2022년 3월 23일
this works too! Thanks!

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

추가 답변 (2개)

Image Analyst
Image Analyst 2022년 3월 23일
What are you sorting on? The average value of each array? If so, try this:
% Make an unsorted G
G = {[-6; -7] [-65] [-15;-16] [-30;-33] [-22;-26] [-47;-50] [-83;-84]}
% Find means
for k = 1 : numel(G)
theMeans(k) = mean(G{k}, 'all');
end
% Sort based on means:
[sortedMeans, sortorder] = sort(theMeans, 'descend')
% Now sort G
GSorted = G(sortorder)
celldisp(GSorted)
  댓글 수: 2
Heirleking
Heirleking 2022년 3월 23일
this works too, thanks!
Image Analyst
Image Analyst 2022년 3월 23일
OK, but you have said it works for multiple answers even though they sort it differently. I sorted by the average value and @_ sorted it by the max value. So which way is it? (In this particular case they're the same though.)

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


Ive J
Ive J 2022년 3월 22일
If by sort, you mean sort within each element of cell array:
G = {[-83;-84] [-65] [-50;-47] [-33;-30] [-26;-22] [-16;-15] [-6; -7]};
Ga = cellfun(@(x)sort(x, 'ascend'), G, 'uni', false);
Gd = cellfun(@(x)sort(x, 'descend'), G, 'uni', false);
Or if you mean overall, vertcat the cell array and then sort.
  댓글 수: 1
Heirleking
Heirleking 2022년 3월 23일
편집: Heirleking 2022년 3월 23일
Hello,
I tried your answer and both Ga and Gd end up being the same.
What I mean is how can I organize G in this way?
G = {[-6; -7] [-15;-16] [-22;-26] [-30;-33] [-47;-50] [-65] [-83;-84]};

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

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by