【问题标题】:Error using == Matrix dimensions must agree错误使用 == 矩阵维度必须一致
【发布时间】:2015-01-20 07:01:14
【问题描述】:

我一直在做一个任务。 我一直在处理的代码的一部分不起作用。 有一个文件叫txt,它是一个12x1的元胞数组,一列12个单词,代码需要逐个调用每个名字,并做一些计算。 我需要第 30 行中的代码来检查单元格是否包含单词 shmoop,如果包含,则执行该代码,否则执行其他代码。 如何检查数组是否是单词 shmoop! 谢谢!

使用 == 时出错 矩阵尺寸必须一致。

asd 中的错误(第 30 行) 如果车道 == 'shmoop';

for c = 2:d
lanes = txt{c,1};
if lanes == 'shmoop';
    Monstershit{d,2}=paces;
    Monstershit{d,3}=pacez;
    Monstershit{d,4}=pacea;
else
    Monstershit{d,2}=pacev;
    Monstershit{d,3}=peace;
    Monstershit{d,4}=pasem;
end

结束

【问题讨论】:

    标签: matlab matrix error-handling dimensions


    【解决方案1】:

    仅当两个对象的尺寸相同时,将字符串与== 进行比较才有效。一般来说,您应该改用strcmp

    【讨论】:

    • 所以我用 if lanes = strcmp(c,1);我需要帮助!还是不行。
    • 你误会了。您应该使用if strcmp(lanes,'shmoop') - strcmp 用于比较两个字符串以查看它们是否相同,如果它们相同则返回true。
    • 好的,我想通了,应该给它一些时间,谢谢。我现在有一个不同的问题。我设置了代码,现在可以检查 0,1 了!现在我有一个不同的问题,如果 word == 1,这段代码不会写入数组 Monstershit(btw,srry for the swear,forgot to change it) if word == 1;大便{d,2}=1; Monstershit{d,3}=randi([3,6]); Monstershit{d,4}=randi([1,3]);
    • 如果您有其他问题,请将其作为单独的问题发布。
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多