具有线性时间查找的字符串数组
我在 Matlab 中进行字符串处理,通常使用元胞数组来存储文本中的各个单词
示例:
a = {'this', 'is', 'an', 'array', 'of', 'strings'}
为了在此数组中搜索单词“of”,我循环遍历该数组并根据我的单词检查每个单独的元素。此方法无法扩展,因为如果我得到一个大数据集,我的数组 a 就会变大,并且循环遍历元素是不明智的。我想知道是否有更聪明的方法,也许是 Matlab 中更好的本机数据结构,可以帮助我更快地运行?
I am doing string processing in Matlab and I usually use cell arrays to store individual words in the text
Example:
a = {'this', 'is', 'an', 'array', 'of', 'strings'}
For searching for the word 'of' in this array I loop through the array and check each individual element against my word. This method does not scale since if I get a large dataset my array a will grow large and looping through elements is not wise. I am wondering if there is any more smart way, perhaps a better native data structure in Matlab, that can help me run this faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
地图容器是一种选择。我不知道您打算执行哪种具体类型的字符串处理,但这里有一个示例,说明如何将每个字符串存储为与元胞数组中该单词的索引位置向量相关联的键:
然后您可以获取单词的索引位置:
或者检查元胞数组中是否存在单词(即它是否是键):
A map container is one option. I don't know what specific sort of string processing you intend to do, but here's an example for how you can store each string as a key which is associated with a vector of index positions of that word in a cell array:
You could then get the index positions of a word:
Or check if a word exists in the cell array (i.e. if it is a key):