Sorting complex strings by a deliminator
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello!
I am currenty sorting a cell array by the first column, which is made up of ID codes stored as strings. The ID's are made up of numbers and characters, separated by dashes. Here is what it looks like (only pay attention to the first column, the rest isn't the actual data):
'c1-3' 247 247 247 97 0 0
'c1-4' 247 247 247 97 0 0
'c1-5' 247 247 247 97 0 0
'c1-1' 247 247 247 97 0 0
'c1-2' 247 247 247 97 0 0
'c2-11' 195 195 195 78 0 0
'c2-4' 195 195 195 78 0 0
'c2-7' 195 195 195 78 0 0
'c2-2' 195 195 195 78 0 0
'c2-9' 195 195 195 78 0 0
'c2-8' 195 195 195 78 0 0
'c2-10' 195 195 195 78 0 0
'c2-6' 195 195 195 78 0 0
'c2-3' 195 195 195 78 0 0
'c2-5' 195 195 195 78 0 0
'c2-1' 195 195 195 78 0 0
'c3-5' 127 127 127 53 5 0
'c3-7' 127 127 127 53 5 0
'c3-2' 127 127 127 53 5 0
'c3-1' 127 127 127 53 5 0
'c3-9' 127 127 127 53 5 0
'c3-6' 127 127 127 53 5 0
'c3-8' 127 127 127 53 5 0
'c3-4' 127 127 127 53 5 0
'c3-3' 127 127 127 53 5 0
This is just my sample code, realistically I would have ID's with more values like '1-4-3-A'
I want it to each section that is separated by a dash and sort by the total value if it's a number, rather than one digit at a time. Unfortunately, the way the regular sortrows function works gives me issues when I have double digit numbers. For example, in this array, I have an issue where c2-10 and c2-11 come before c2-2 so rather than getting this:
'c1-1' 247 247 247
'c1-2' 247 247 247
'c1-3' 247 247 247
'c1-4' 247 247 247
'c1-5' 247 247 247
'c2-1' 195 195 195
'c2-2' 195 195 195
'c2-3' 195 195 195
'c2-4' 195 195 195
'c2-5' 195 195 195
'c2-6' 195 195 195
'c2-7' 195 195 195
'c2-8' 195 195 195
'c2-9' 195 195 195
'c2-10' 195 195 195
'c2-11' 195 195 195
'c3-1' 127 127 127
'c3-2' 127 127 127
'c3-3' 127 127 127
'c3-4' 127 127 127
'c3-5' 127 127 127
'c3-6' 127 127 127
'c3-7' 127 127 127
'c3-8' 127 127 127
'c3-9' 127 127 127
I get this:
'c1-1' 247 247 247
'c1-2' 247 247 247
'c1-3' 247 247 247
'c1-4' 247 247 247
'c1-5' 247 247 247
'c2-1' 195 195 195
'c2-10' 195 195 195
'c2-11' 195 195 195
'c2-2' 195 195 195
'c2-3' 195 195 195
'c2-4' 195 195 195
'c2-5' 195 195 195
'c2-6' 195 195 195
'c2-7' 195 195 195
'c2-8' 195 195 195
'c2-9' 195 195 195
'c3-1' 127 127 127
'c3-2' 127 127 127
'c3-3' 127 127 127
'c3-4' 127 127 127
'c3-5' 127 127 127
'c3-6' 127 127 127
'c3-7' 127 127 127
'c3-8' 127 127 127
'c3-9' 127 127 127
Does anyone know if a fuction exists within matlab that would sort it the way I'm looking for?
0 commentaires
Réponses (1)
Bruno Luong
le 21 Août 2020
One way
c = {'c1-3' 247 247 247 97 0 0
'c1-4' 247 247 247 97 0 0
'c1-5' 247 247 247 97 0 0
'c1-1' 247 247 247 97 0 0
'c1-2' 247 247 247 97 0 0
'c2-11' 195 195 195 78 0 0
'c2-4' 195 195 195 78 0 0
'c2-7' 195 195 195 78 0 0
'c2-2' 195 195 195 78 0 0
'c2-9' 195 195 195 78 0 0
'c2-8' 195 195 195 78 0 0
'c2-10' 195 195 195 78 0 0
'c2-6' 195 195 195 78 0 0
'c2-3' 195 195 195 78 0 0
'c2-5' 195 195 195 78 0 0
'c2-1' 195 195 195 78 0 0
'c3-5' 127 127 127 53 5 0
'c3-7' 127 127 127 53 5 0
'c3-2' 127 127 127 53 5 0
'c3-1' 127 127 127 53 5 0
'c3-9' 127 127 127 53 5 0
'c3-6' 127 127 127 53 5 0
'c3-8' 127 127 127 53 5 0
'c3-4' 127 127 127 53 5 0
'c3-3' 127 127 127 53 5 0 }
x=cellfun(@(s) textscan(s,'c%d-%d'), c(:,1),'unif', 0);
x=[x{:}];
x=reshape([x{:}],2,[])';
[~,is]=sortrows(x);
c(is,:)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!