how to sort the rows in an cell array.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
jaah navi
am 24 Jun. 2021
Kommentiert: jaah navi
am 25 Jun. 2021
I am having cell array of size 3x1
[5,6]
[1,2]
[5,8]
could anyone help me how to sort the rows to get the result in the following manner
[1,2]
[5,6]
[5,8]
1 Kommentar
Scott MacKenzie
am 24 Jun. 2021
It would help if you posted the code that generates the cell array you want to sort.
Akzeptierte Antwort
Mohammad Sami
am 25 Jun. 2021
You can try this out.
B = {[5,6] [1,2] [5,8]};
[~,i] = sort(cellfun(@(x)sum(x.*power(10,[length(x):-1:1]-1)),B));
B = B(i)
3 Kommentare
Mohammad Sami
am 25 Jun. 2021
This is just converting the array into a decimal value so [5,6] to 56. So makes it easier to sort the numbers. Though you can replace it with the maximum value you expect to be present in your array and it should still work well enough.
B = {[5,6] [1,2] [5,8]};
cellfun(@(x)sum(x.*power(10,[length(x):-1:1]-1)),B)
Weitere Antworten (0)
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!