比较单元格数组中的字符串 [英] Comparing strings in cell arrays
问题描述
我想在单词列表中找到最常用的单词。这是我的代码到目前为止:
uniWords = unique
for i = 1:length(words)
for j = 1:length(uniWords)
if(uniWords(j)== lower(words(i)))
freq(j)= freq(j)+ 1;
end
end
end
当我尝试运行脚本,我得到以下错误:
类型'单元格'的输入参数的未定义函数'eq'。
错误Biweekly3(行106)
if(uniWords(j)== lower(words(i)))
感谢任何帮助。
我想你需要做:
if(uniWords {j} == lower(words {i}))
此外,我建议在MATLAB中不使用 更新 如Chappjc指出,最好使用 I'm trying to find the most frequent word in a list of words. Here is my code so far: When I try to run the script, I get the following error: Any help is appreciated! I guess you need to do: Also, I suggest not using Update As Chappjc points out, it is better to use 这篇关于比较单元格数组中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! i
和 j
p>
strcmp
(或在您的情况下 strcmpi
并跳过 lower
),因为您想忽略大小写。 uniWords = unique(lower(words));
for i = 1:length(words)
for j = 1:length(uniWords)
if (uniWords(j) == lower(words(i)))
freq(j) = freq(j) + 1;
end
end
end
Undefined function 'eq' for input arguments of
type 'cell'.
Error in Biweekly3 (line 106)
if (uniWords(j) == lower(words(i)))
if (uniWords{j} == lower(words{i}))
i
and j
as variables in MATLAB.strcmp
(or in your case strcmpi
and skip lower
), since you want to ignore cases.