【问题标题】:Iterating Over Unique Values in Matlab在 Matlab 中迭代唯一值
【发布时间】:2015-05-08 08:29:58
【问题描述】:

我一直在尝试关注this answer,以便从给定的元胞数组中获取唯一的字符串。但是,在迭代这些值时我遇到了麻烦。我尝试了如下循环:

[unique_words, ~, occurrences] = unique(words);
unique_counts = hist(occurrences, 1:max(occurrences));

for a=1:numel(unique_words)
  word = unique_words{a}
  count = unique_counts{a}
  result = result + a_struct.(unique_words{a}) + unique_counts{a}
end

当试图引用这样的项目时,我收到错误:

来自非元胞数组对象的元胞内容引用。

unique_couts 的大括号更改为圆括号会产生错误:

引用不存在的字段“N1”。

unique_wordsunique_counts 都更改为圆括号会产生:

动态结构引用的参数必须评估为有效的字段名称。

我如何迭代 unique 的结果?

【问题讨论】:

    标签: matlab text brackets curly-braces word-frequency


    【解决方案1】:

    unique_words 是一个元胞数组。 unique_counts 是一个向量。所以unique_words 应该使用大括号访问,unique_counts 使用圆括号访问。在这种情况下您遇到的错误与 a_struct (问题中未定义)没有相应的字段有关,而不是访问方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多