Multiple conditional statements with logic

6 次查看(过去 30 天)
I want to write a function called under_age that inputs whether a person is too young or not. It has multiple inputs of age and limit.
If the age is less than the limit than too_young = true and if age is greater than too_young = false. I also want to put in a case when the age is inputed
In which case, the age is of the limit of automatically set to 21.
At the moment the function works if the person is too_young = true. But I can't figure out why it doesn't work when too_young = false.
function [too_young] = under_age(age, limit)
if (nargin == 2)
age < limit
too_young = true;
elseif (nargin == 2)
age >= limit
too_young = false;
elseif (nargin == 1) || isempty(limit)
age < 21
too_young = true;
elseif (nargin == 1) || isempty(limit)
age >= 21
too_young = false;
end
  1 个评论
Image Analyst
Image Analyst 2020-5-26
Apparently this is homework, because it's a duplicate of this question. I will label it as homework, like it should have been originally so that no one gave complete solutions.

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2020-3-31
编辑:Image Analyst 2020-5-17
Try this:
too_young = under_age(22, 21)
too_young = under_age(11, 25)
too_young = under_age(33)
too_young = under_age(18)
function too_young = under_age(age, limit)
fprintf('nargin = %d\n', nargin);
if (nargin == 2)
too_young = age < limit; % Returns true or false.
else
too_young = age < 21; % Returns true or false.
end
end
  2 个评论
Image Analyst
Image Analyst 2020-5-17
Alright, I've added comments. So many that there is now more comments than code. It should be even more self-explanatory now.
% Top part of m-file:
% Call the under_age() function with various values and number of input arguments.
% First call with two input arguments, each with a different limit.
too_young = under_age(22, 21)
too_young = under_age(11, 25)
% Now call with no second input argument. The under_age() function will assume a limit of 21.
too_young = under_age(33)
too_young = under_age(18)
% Bottom part of m-file, the function definition.
function too_young = under_age(age, limit)
% Print out how many input arguments the user called the function with.
fprintf('nargin = %d\n', nargin);
if (nargin == 2)
% The function was called with 2 input arguments.
% We need to see if the age (first argument) is below or above limit (the second argument).
too_young = age < limit; % Returns true or false.
else
% The function was called with only one argument.
% So we need to check only this one age against a value of 21, not against any other value.
too_young = age < 21; % Returns true or false.
end
end % Final end is required if the function is in the same m-file as the script, but not if it's all by itself in it's own m-file.

请先登录,再进行评论。


Alberto Chavez
Alberto Chavez 2020-3-31
I think you have part of your expressions in the statements section.
Maybe this works.
function [too_young] = under_age(age, limit)
if (nargin == 2) && age < limit
too_young = true;
elseif (nargin == 2) && age >= limit
too_young = false;
elseif (nargin == 1) && isempty(limit) && age < 21
too_young = true;
elseif (nargin == 1) && isempty(limit) && age >= 21
too_young = false;
end
  3 个评论
Image Analyst
Image Analyst 2020-3-31
You don't need all that complicated stuff. Did you try my code? It works fine, either for one or two inputs.
Alberto Chavez
Alberto Chavez 2020-4-1
编辑:Alberto Chavez 2020-4-1
Oh my bad, this should work.
function [too_young] = under_age(age,limit)
if nargin==2
if isempty(limit)==1
if age<21
too_young = true;
elseif age>=21
too_young = false;
end
elseif isempty(limit)==0
if age<limit
too_young = true;
elseif age>=limit
too_young = false;
end
end
elseif nargin==1
if age<21
too_young = true;
elseif age>=21
too_young = false;
end
end
I had to restructure your code, you have 3 levels of possibilities in your inputs. First whether you have:
under_age(age,limit)
%% or
under_age(age)
%% Second, whether you have
limit=[] % or
limit=anynumber
%% Third, whether you have
age<limit || 21 % or
age>=limit || 21
I ran it on my PC and it works now.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Control System Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by