MATLAB中单元格中正则表达式的字符数 [英] Character count of regular expression in cells in MATLAB
问题描述
之前,我对如何制作一个脚本可以从推文列表中提取主题标签并将其放入单元格数组中有所帮助. 我将此作为我的代码,放在for循环中
Earlier I got some help as to how to make a script that will extract hashtags from a list of tweets and put them into an array of cells. I used this as my code, inside a for loop
hashtagCell{i} = regexp(textRead{i}, '#[A-z]*', 'match');
这可以完成预期的工作,但是现在我试图查找主题标签的平均字符长度,因此我需要能够添加通过上述函数提取的每个主题标签的字符长度,并将它们添加在一起.但是,当我尝试使用size()函数时,它只是给我单元格的大小,而不是字符串的大小,这正是我想要的.我不知道该怎么做.
This works for what it is supposed to do, but now I'm trying to find the average character length of the hashtags, so I need to be able to add the character length of each hashtag pulled out by the above function and add them together. However, when I try to use the size() function, it just gives me the size of the cell instead of the size of the strings, which is what I want. I can't figure out how to do this.
推荐答案
这应该有所帮助(它可以摆脱任何循环,除了可能不是用来创建CellOfText
的循环):
This should help (and it gets rid of any loops, other than, perhaps, the one used to create CellOfText
):
%# Example cell array of tweets
CellOfText = {'Bah #humbug says #Mr scrooge'; 'No #presents for you'};
%# Get all hash tags
HTC = regexp(CellOfText, '#[A-z]*', 'match');
%# Get the average hash tag length, being careful to unnest HTC
AvgLength1 = mean(cellfun('length', [HTC{:}]));
DISCLAIMER: The inspiration for this method came from this excellent answer to a similar question. Thanks to @Andrey for that.
这篇关于MATLAB中单元格中正则表达式的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!