本文介绍了如何计算字符或MATLAB中的唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
那么怎么算没有。重复字母出现在某些阵列>
例如,我有一个数组
A一个一个CbCCð一个我怎么能知道如何可能的a,b,c和发生的?我想这样的输出:
字母数4ç3b 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 ahow 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 1so 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中的唯一字符串
发布评论