Sort repetition in text
Show older comments
I'm trying to sort a string of letters into repetition of each letter.
for example:
Text= 'KKKKLLOOKkl';
Result should be {'4K','2L','2O','1K,'1k','1l'}
- Text could be any length.
- Text could be only alphabet letters (capital and small).
- Result could be any type of array (vector/ table/ cell ect).
I used 'unique' to get which letters are inside Text but how do I count the repetition for each occurence?
Is there a fast way or a function instead of looping for each result of unique?
Text = 'KKKKLLOOKkl';
[C,~,Xz] = unique(Text,'stable')
C = 'KLOkl'
Xz = 1,1,1,1,2,2,3,3,1,4,5
Thanks for any help :)
Accepted Answer
More Answers (1)
T = 'KKKKLLOOKkl';
D = [true,diff(T)~=0];
L = diff(find([D,true]));
C = compose('%s%d',T(D).',L(:))
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!