Problem Multiplying- mtimes doesn't work for cells
5 次查看(过去 30 天)
显示 更早的评论
Hey, guys! I'm trying to get this program to recognize certain keywords in a sentence, and then based on those words, it will assign values to "a" and "b", and then multiply the two to give an answer.
Let me explain. If I write "helping your brother", the program should identify "Helping" as part of Good and then assign "a" a value of 1. it should also recognize "Brother" as part of Good and assign "b" a value of 1. At the end, the program should multiply these two numbers, and since a*b = 1, I should get something out of the pool of GoodResp[onses] like "that's good"
however, there's an error, and I'm boggled. "Any help here would be hot" thank you!
Error ------------
??? Undefined function or method 'times' for input
arguments of type 'cell'.
Error in ==> goodbad2 at 29
prod = a.*b
---------
Code:
GoodObject = {'brother', 'sister', 'mother', 'father', };
BadObject = {'killer','murderer', 'robber', 'thief'};
Bad = {'hurting', 'stealing', 'robbing', 'breaking'};
Good = {'helping', 'giving', 'caring'};
question = input('what? ','s');
if any(~cellfun(@isempty,regexpi(question,GoodObject)))
a = 1;
else if any(~cellfun(@isempty,regexpi(question,BadObject)))
a = -1;
else if any(~cellfun(@isempty,regexpi(question,Good)))
b = 1;
else if any(~cellfun(@isempty,regexpi(question,GoodObject)))
b = -1;
end
end
end
end
result = a*b
BadResp = {'that''s bad', 'that''s very not good', 'that''s pretty bad'};
GoodResp = {'that''s very good', 'yep, that''s good', 'that''s good'};
OutBad = BadResp(randi(numel(BadResp)));
OutGood = GoodResp(randi(numel(GoodResp)));
if result == -1
disp(OutBad)
else if result == 1
disp(OutGood)
end
end
1 个评论
per isakson
2013-6-27
编辑:per isakson
2013-6-27
Insert the line
a, b
above
prod = a.*b;
and tell us what it shows.
BTW: should be elseif in one word
采纳的回答
Evan
2013-6-27
编辑:Evan
2013-6-27
Two things:
1) You might want to change the variable name "prod" to something else. "Prod" is a built-in matlab function that performs multiplication. Giving variables the names of built-in functions isn't recommended.
2) Is either a or b used at any point earlier in your code (perhaps in a part not included here)? The error is saying that at least one of your variables being multiplied is a cell array. If a and b were to both get set in your if/elseif statements, you wouldn't be getting this error.
It looks like, for your inputs, only a or b is set, not both of them. This means you'll either get an undefined function or variable error ... or, if the variable name is used elsewhere, the variable wont be overwritten and your function will be trying to multiply the old variable, which is possibly a cell array.
NOTE: I just noticed that this is a raw script, not a function, so, since only either a or b gets set, if you at any point create a "b" variable in your workspace before running this code, you're going to run into problems. So you should 1) Make this a function or make sure you're clearing any variables that might interfere AND 2) fix your script so that both a and b are set, perhaps by changing your nested if/else statements to their own standalone if/end statements.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!