Effacer les filtres
Effacer les filtres

Sorting cell array of strings wrt. a character

1 vue (au cours des 30 derniers jours)
Ole Hansen
Ole Hansen le 5 Juin 2012
Say I have:
SM={'JJ','AKs','TT','AQs','AKo','AJs','KQs','99','ATs','AQo'}
and I would like to sort it wrt. {XXs}, {YYo} and {the remaining strings} (i.e. strings without 's' or 'o'). How can that be done?

Réponse acceptée

Sean de Wolski
Sean de Wolski le 5 Juin 2012
SM={'JJ','AKs','TT','AQs','AKo','AJs','KQs','99','ATs','AQo'}
idx = cellfun(@(x)any(ismember('so',x)),SM); %which ones have s or o?
v = 1:numel(SM); %index vector
SM([v(~idx) v(idx)]) %extract

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 5 Juin 2012
k = cell2mat(cellfun(@(x)find(~cellfun('isempty',regexp(SM,x))),{'s' 'o'},'un',0))
out = SM([k setdiff(1:numel(SM),k)])

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by