Problem Multiplying- mtimes doesn't work for cells

4 vues (au cours des 30 derniers jours)
kenny
kenny le 27 Juin 2013
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 commentaire
per isakson
per isakson le 27 Juin 2013
Modifié(e) : per isakson le 27 Juin 2013
Insert the line
a, b
above
prod = a.*b;
and tell us what it shows.
BTW: should be elseif in one word

Connectez-vous pour commenter.

Réponse acceptée

Evan
Evan le 27 Juin 2013
Modifié(e) : Evan le 27 Juin 2013
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.
  2 commentaires
kenny
kenny le 27 Juin 2013
I changed "prod" to "result". thanks for that tip. I had to change the formatting of the "if" statement... thank you !
Evan
Evan le 27 Juin 2013
You're welcome! Glad you got it sorted out. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by