具有线性时间查找的字符串数组

发布于 2024-12-28 20:53:48 字数 279 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2025-01-04 20:53:48

地图容器是一种选择。我不知道您打算执行哪种具体类型的字符串处理,但这里有一个示例,说明如何将每个字符串存储为与元胞数组中该单词的索引位置向量相关联的键:

a = {'this', 'is', 'an', 'array', 'of', 'strings', 'this', 'is'};

strMap = containers.Map();  %# Create container
for index = 1:numel(a)      %# Loop over words to add
    word = a{index};
    if strMap.isKey(word)
        strMap(word) = [strMap(word) index];  %# Add to an existing key
    else
        strMap(word) = index;  %# Make a new key
    end
end

然后您可以获取单词的索引位置:

>> indices = strMap('this')

indices =

     1     7    %# Cells 1 and 7 contain 'this'

或者检查元胞数组中是否存在单词(即它是否是键):

>> strMap.isKey('and')

ans =

     0    %# 'and' is not present in the cell array

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:

a = {'this', 'is', 'an', 'array', 'of', 'strings', 'this', 'is'};

strMap = containers.Map();  %# Create container
for index = 1:numel(a)      %# Loop over words to add
    word = a{index};
    if strMap.isKey(word)
        strMap(word) = [strMap(word) index];  %# Add to an existing key
    else
        strMap(word) = index;  %# Make a new key
    end
end

You could then get the index positions of a word:

>> indices = strMap('this')

indices =

     1     7    %# Cells 1 and 7 contain 'this'

Or check if a word exists in the cell array (i.e. if it is a key):

>> strMap.isKey('and')

ans =

     0    %# 'and' is not present in the cell array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文