필터 지우기
필터 지우기

Problem Multiplying- mtimes doesn't work for cells

조회 수: 10 (최근 30일)
kenny
kenny 2013년 6월 27일
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
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
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.
  댓글 수: 2
kenny
kenny 2013년 6월 27일
I changed "prod" to "result". thanks for that tip. I had to change the formatting of the "if" statement... thank you !
Evan
Evan 2013년 6월 27일
You're welcome! Glad you got it sorted out. :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by