MATLAB中单元格中正则表达式的字符数 [英] Character count of regular expression in cells in MATLAB

查看:76
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆