Multiple conditional statements with logic

조회 수: 2 (최근 30일)
Thomas Sun
Thomas Sun 2020년 3월 31일
댓글: Image Analyst 2020년 5월 26일
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
Abhiram Ram
Abhiram Ram 2020년 5월 17일
can you explain.your code
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