比较单元格数组中的字符串 [英] Comparing strings in cell arrays

查看:184
本文介绍了比较单元格数组中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单词列表中找到最常用的单词。这是我的代码到目前为止:

  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中不使用 i j p>

更新



如Chappjc指出,最好使用 strcmp (或在您的情况下 strcmpi 并跳过 lower ),因为您想忽略大小写。


I'm trying to find the most frequent word in a list of words. Here is my code so far:

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

When I try to run the script, I get the following error:

Undefined function 'eq' for input arguments of
type 'cell'.

Error in Biweekly3 (line 106)
    if (uniWords(j) == lower(words(i)))

Any help is appreciated!

解决方案

I guess you need to do:

if (uniWords{j} == lower(words{i}))

Also, I suggest not using i and j as variables in MATLAB.

Update

As Chappjc points out, it is better to use strcmp (or in your case strcmpi and skip lower), since you want to ignore cases.

这篇关于比较单元格数组中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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