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

编程入门 行业动态 更新时间:2024-10-18 03:26:51
本文介绍了如何计算字符或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 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中的唯一字符串

本文发布于:2023-11-29 12:36:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   MATLAB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!