Matlab 计算字符串中重复的字母数

Matlab 计算字符串中重复的字母数,matlab,Matlab,我试图找出每个字母在单元格数组中出现的次数 我必须用Matlab打开这个数据文件 A 12 A 88 B 23 F 22 C 55 B 77 D 66 H 44 fid = fopen('Thor.dat') if fid == -1 disp ('File open not successful') else disp ('File open is successful') mat = textscan(fid,'%c %f %c %f') [r c] =

我试图找出每个字母在单元格数组中出现的次数

我必须用Matlab打开这个数据文件

A 12 A 88
B 23 F 22
C 55 B 77
D 66 H 44
fid = fopen('Thor.dat')

if fid == -1
    disp ('File open not successful')
else
    disp ('File open is successful')
    mat = textscan(fid,'%c %f %c %f')
    [r c] = size(mat)
    charoccur(mat)        
    fclose(fid)
end
我把它命名为Thor.dat,这是我在Matlab中的代码

A 12 A 88
B 23 F 22
C 55 B 77
D 66 H 44
fid = fopen('Thor.dat')

if fid == -1
    disp ('File open not successful')
else
    disp ('File open is successful')
    mat = textscan(fid,'%c %f %c %f')
    [r c] = size(mat)
    charoccur(mat)        
    fclose(fid)
end
并且
charoccure
函数为

function occurence = charoccur(mat)
% charoccur finds the number of times each character appears in a column

[row, col] = size(mat);
[row, ccol] = size(mat{1});

[mat] = unique(mat{i})
d = hist(c,length(a))
end

下面是一种使用
unique
和的方法。基本上循环遍历单元格数组,并求每个唯一字母的出现次数之和。使用strcmp可以得到0和1的逻辑数组。通过对1求和,可以得到一个字母被找到的总次数

clear
clc


%// Input cell array
mat = {'A' 12 'A' 88;'B' 23 'F' 22;'C' 55 'B' 77;'D' 66 'H' 44;'W' 11 'C' 9;'H' 3 'H' 0};

mat = [mat(:,1) mat(:,3)]

%// Find unique letters
UniqueLetters = unique(mat);

%// Initialize output cell
OutputCell = cell(numel(UniqueLetters,2));

%// Loop through each unique letter and count number of occurence
for k = 1:numel(UniqueLetters);
    %// 1st column: letter
    OutputCell(k,1) = UniqueLetters(k);

    %// 2nd column: sum of occurences
    OutputCell(k,2) = {sum(sum(strcmp(UniqueLetters{k},mat)))};

end

OutputCell
OutputCell
现在看起来像这样:

OutputCell = 

    'A'    [2]
    'B'    [2]
    'C'    [2]
    'D'    [1]
    'F'    [1]
    'H'    [3]
    'W'    [1]
希望这能帮助你开始

编辑:

根据您的评论,对于输出单元格数组的初始化:

OutputCell = cell(numel(UniqueLetters,2));
我创建了一个Nx2单元数组,其中N(行数)是通过调用
unique
标识的唯一单元数。在上面的示例中,有7个唯一的字母,因此我创建了一个7x2单元格数组来存储:

1) 在第1栏中,实际字母


2) 在第2列中,这封信在
mat

中找到的次数非常感谢。但是当我运行这个代码时,它给了我一个错误:???在52处使用==>cell.unique时出错。数组中的每个元素都必须是单行文本数组。如果只在向量中保留足够大的计数以索引任何有效字符,则不需要先使用unique。但这听起来不像是通过计数字符来获得频率,而是通过解析一个已经存在的频率表。你只需要处理你的
mat
单元格数组中的文本元素。@BenVoigt oh我没有看到这样的结果,也许我真的错了。或者我可能是一个误解。希望问题得到澄清。预期的结果是什么?类似这样的:'A'[2]'B'[2]'C'[2]'D'[1]'F'[1]'H'[3]'W'[1]我编辑了你的问题标题,并重新格式化了一些部分,以便于阅读。