如何计算字符或MATLAB中的唯一字符串 [英] How to count characters or unique string in matlab
本文介绍了如何计算字符或MATLAB中的唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
那么怎么算没有。重复字母出现在某些阵列>
例如,我有一个数组
A
一个
一个
C
b
C
C
ð
一个
我怎么能知道如何可能的a,b,c和发生的?我想这样的输出:
字母数
4
ç3
b 1分配
ð1
那么,如何能做到这一点?谢谢
解决方案
改编= {'A''A''A''C''B''C''C' 'D''A'}//%字母映射带有数字和尽数
数= HIST(cellfun(@(x)x - 96,ARR))//%过滤结果,并转换为细胞
countCell = num2cell(COUNT(FIND(计数))。')%%//获取排序独特的字母列表
字母=唯一的(ARR)。 %//%产量
outpur = [字母countCell]
在该解决方案的duplicate答案 非常整齐,适用于您所需的输出:
[信件,〜,潜艇] =唯一的(ARR)
countCell = num2cell(accumarray(潜艇(:),1,[],@总和))
输出= [字母'。 countCell]
在我看来,你的输入数组,而如下:
改编='A'; '一个'; '一个'; 'C'; 'B'; 'C'; 'C'; 'D'; '一个']
所以更改最后一行:
输出= [cellstr(字母)countCell]
输出= 一个[4]
'B'[1]
的'c'[3]
'D'[1]
So how to count no. of repeated letters occur in certain array>
for example i have a array
a
a
a
c
b
c
c
d
a
how can i know how may a,b,c,and occur? i want an output like this:
Alphabet count
a 4
c 3
b 1
d 1
so how can i do that? thanks
解决方案
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]
The solution in the duplicate answer is very neat, applied to your desired output:
[letters,~,subs] = unique(arr)
countCell = num2cell(accumarray(subs(:),1,[],@sum))
output = [letters.' countCell]
It appears to me, that your input array rather looks like:
arr = ['a'; 'a'; 'a'; 'c'; 'b'; 'c'; 'c'; 'd'; 'a']
so change the last line to:
output = [cellstr(letters) countCell]
output =
'a' [4]
'b' [1]
'c' [3]
'd' [1]
这篇关于如何计算字符或MATLAB中的唯一字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文