File Exchange

image thumbnail

String Letter Counter Function

version 1.1.0.0 (1.35 KB) by Avinash Rude
This function returns comma separated unique letter counts in an input string, keeping the order.

0 Downloads

Updated 07 Jan 2010

View License

Statement: strLetterCounter('string')
....where, 'string' is an input string containing alphabet or numbers.

Ex: string='aaaaaaaabbbbbbccd'
Statement: strLetterCounter(string)
Output: '8a,6b,2c,1d'

Note: This function is different from the Matlab function called 'unique' because it keeps the order in which letters occur in the string.

Cite As

Avinash Rude (2020). String Letter Counter Function (https://www.mathworks.com/matlabcentral/fileexchange/26295-string-letter-counter-function), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

This submission has pretty poor code: no array preallocation and the use of (slow) function "num2str" inside a "for" loop means that this will get very slow for long strings with many different characters. The string output is not very practical for further calculations, and no indices is returned. Minimal help in the Mfile, with no input/output descriptions.

Below are two alternatives which accept both numeric and char arrays, are fully vectorized (no "for" loop), also return the sort indices, and are faster too: "count_unique" is ~2x, while "count_group" is ~10x. You can find other alternatives on FEX.

% start code "count_group"
function [N,C,ia,ic] = count_group(A)
% Return the unique elements of an array and their total count, preserving order and group.
%
% Syntax:
% [N,C,ia,ic] = count_group(A)
%
% Examples:
%
% [N,C] = count_group('yyyymmddy')
% N = [4,2,2,1]
% C = 'ymdy'
%
% [N,C,ia,ic] = count_group([0,1,0;1,2,0])
% N = [1,2,1,2]
% C = [0,1,2,0]
% ia = [1,2,4,5]
% ic = [1,2,4;2,3,4]
%
% Inputs:
% A = NumericArray or CharArray
% Outputs:
% C = The unique elements of <X>, preserving the order.
% N = NumericVector, the count of each element in <C>.
% ia = NumericVector, index such that C=A(ia).
% ic = NumericVector, index such that A=C(ic).

siz = size(A);
A = A(:).';
assert(ischar(A)||isnumeric(A)&&all(isfinite(A)),'Input must be a char or numeric.')
ic = [1,diff(A)];
ia = find(ic);
ic = reshape(cumsum(ic~=0),siz);
C = reshape(A(ia),1,[]);
N = diff([ia,1+numel(A)]);
% end code "count_group"

% start code "count_unique"
function [N,C,ia,ic] = count_unique(A)
% Return the unique elements of an array and their total count, preserving order.
%
% Syntax:
% [N,C,ia,ic] = count_unique(A)
%
% Examples:
%
% [N,C] = count_unique('yyyymmddy')
% N = [5,2,2]
% C = 'ymd'
%
% [N,C,ia,ic] = count_unique([0,2,0;2,1,0])
% N = [3,2,1]
% C = [0,2,1]
% ia = [1,2,4]
% ic = [1,2,1;2,3,1]
%
% Inputs:
% A = NumericArray or CharArray
% Outputs:
% N = NumericVector, the count in <A> of each element in <C>.
% C = The unique elements of <A>, preserving the order.
% ia = NumericVector, index such that C=A(ia).
% ic = NumericVector, index such that A=C(ic).

siz = size(A);
A = A(:).';
assert(ischar(A)||isnumeric(A)&&all(isfinite(A)),'Input must be a char or numeric.')
[z,y,x] = unique(A,'first');
[ia,v] = sort(y);
if isscalar(z)
N = numel(A);
else
w = hist(+A,+z);
N = w(v);
end
C = A(ia);
ic = reshape(v(x),siz);
% end code "count_unique"

Oleg Komarov

Poor preallocation. Complex output. Poor help.
>> strLetterCounter('aaaaaaaabbbbbbccdaaa')
ans =
8a,6b,2c,1d,3a
This fcn counts the length of continues blocks.

Oleg

Updates

1.1.0.0

Fixed printing of a local variable.

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux