Sort cell numbers ?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Heirleking
am 22 Mär. 2022
Kommentiert: Image Analyst
am 23 Mär. 2022
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]}
0 Kommentare
Akzeptierte Antwort
Voss
am 23 Mär. 2022
Bearbeitet: Voss
am 23 Mär. 2022
"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);
Weitere Antworten (2)
Image Analyst
am 23 Mär. 2022
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 Kommentare
Image Analyst
am 23 Mär. 2022
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
am 22 Mär. 2022
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 Kommentar
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!