MATLAB字计数器

时间:2015-12-02 04:19:11

标签: string matlab

我需要编写一个代码来计算字符串中每个单词的出现次数。如果字符串有多行,我将每一行视为一个单独的字符串。这是我到目前为止所拥有的。我知道在找到匹配的2个单词后,我更新了出现次数并将第二个单词更改为空字符串,因此它不会再次匹配。我的代码不会执行每个单词,它会执行整个字符串。任何人都可以帮我吗?我也知道我需要使用regexp(),但我不确定如何。

[row col] = size(str);
if(row > 1)
    fprintf('You inputted %.0f strings\n',row);
    cArr = cellstr(str);
    for index=1:row-1
        for index2=2:row-1
            numOcc = 1;
            x = cArr(index); 
            y = cArr(index2);
            isSame = strcmp(x,y);
            if isSame == 1
                numOcc = numOcc + 1;
                y = '';
            end;
        end;
    end;
    z = char(x); 
    fprintf('%s: %.0f\n',x,numOcc);
end;

Example output:
str =
This string should be the first row.
This string should be the second row, or should it?
>> wordhist(str)
You inputted 2 strings.
String 1
This: 1
string: 1
should: 1
be: 1
the: 1
first: 1
row: 1
String 2
This: 1
string: 1
should: 2
be: 1
the: 1
second: 1
row: 1
or: 1
it: 1

1 个答案:

答案 0 :(得分:1)

这是一个只能一次处理一个字符串的示例,并不担心标点符号(它只是在空格上分割)。你必须按照自己喜欢的方式处理这两件事。此示例主要取自Matlab Central question

% an example string
str = 'This string is an example string to test if there is a working string counter.'
% the words in the string (use regexp for more advanced e.g., punctuation)
words = strsplit(str, ' ');
% unique words in the cell array
[uwords, idw, idu] = unique(words);
% counts for each word corresponding to idw
n = accumarray(idu,1);
% display the counts
for i = 1:numel(n)
  fprintf('%s: %i\n', words{idw(i)}, n(i));
end

输出结果为:

str =
  This string is an example string to test if there is a working string counter.

This: 1
a: 1
an: 1
counter.: 1
example: 1
if: 1
is: 2
string: 3
test: 1
there: 1
to: 1
working: 1