如何计算matlab中的字符或唯一字符串

How to count characters or unique string in matlab

那么怎么算没有。重复字母出现在特定数组中>

例如我有一个数组

a
a
a
c
b
c
c
d
a

我怎么知道 a、b、c 和如何发生?我想要这样的输出:

Alphabet   count
a           4
c           3
b           1
d           1

那我该怎么做呢?谢谢

arr = {'a' 'a' 'a' 'c' 'b' 'c' 'c' 'd' 'a'}

%// map letters with numbers and count them
count = hist(cellfun(@(x) x - 96,arr))

%// filter result and convert to cell
countCell = num2cell(count(find(count)).') %'

%// get sorted list of unique letters 
letters = unique(arr).' %'

%// output
outpur = [letters countCell]

duplicate answer 中的解决方案非常简洁,适用于您想要的输出:

[letters,~,subs] = unique(arr)
countCell = num2cell(accumarray(subs(:),1,[],@sum))
output = [letters.' countCell]

在我看来,您的输入数组看起来像:

arr = ['a'; 'a'; 'a'; 'c'; 'b'; 'c'; 'c'; 'd'; 'a']

因此将最后一行更改为:

output = [cellstr(letters) countCell]

output = 

    'a'    [4]
    'b'    [1]
    'c'    [3]
    'd'    [1]