フィルターのクリア

How do you have a logical operator of true and false as your type but 0 and 1 as your value?

16 ビュー (過去 30 日間)
Hi. I am having trouble with this homework question: Write a function called eligible that helps the admission officer of the Graduate School of Vanderbilt University decide whether the applicant is eligible for admission based on GRE scores. The function takes two positive scalars called v and q as input. They represent the percentiles of the verbal and quantitative portions of the GRE respectively. You do not need to check the input. The applicant is eligible if the average percentile is at least 92% and both of the individual percentiles are over 88%. The function returns the logical true or false. What I have attempted is below:
function [true, false] = eligible(v, q)
if mean(v, q)>= 92 && v>88 && q>88
fprintf('true\n');
elseif mean(v, q)<92
fprintf('false\n');
elseif v<=88
fprintf('false\n');
elseif q<=88
fprintf('false\n');
I think my problem is that I need the false to appear as the type but 0 to appear as my value and true to appear as the type but 1 to appear as my value.
  2 件のコメント
Pranav nair
Pranav nair 2020 年 7 月 17 日
function admit = eligible(v,q)
c = (v+q)/2;
if (c>= 92 && v>88 && q>88)
admit = true;
else (v < 88 && q < 88)
admit = false;
end

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2016 年 8 月 8 日
You used mean() incorrectly. Look at this:
>> mean(10,30) % WRONG!
ans =
10
>> mean([10,30]) % Right way uses brackets.
ans =
20
So instead of
if mean(v, q)>= 92 && v>88 && q>88
fprintf('true\n');
elseif mean(v, q)<92
fprintf('false\n');
elseif v<=88
fprintf('false\n');
elseif q<=88
fprintf('false\n');
use
if mean([v, q]) >= 92 && v > 88 && q > 88
fprintf('true\n');
isEligible = true;
else
fprintf('false\n');
isEligible = false;
end
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 2 月 17 日
function a=eligible(v,q)
c=(v+q)/2;
a = (c>=92)&&(v>88&&q>88);
the cyclist
the cyclist 2018 年 2 月 18 日
Or, if you are playing Cody ...
function ans = eligible(v,q)
((v+q)>=184)&&(v>88&&q>88);
[This is not a serious comment. Please do not take it as such!]

サインインしてコメントする。

その他の回答 (7 件)

the cyclist
the cyclist 2016 年 8 月 8 日
You are on the right track. Here is a hint ...
Instead of outputting two variables named true and false, I think you'd want to just output one variable -- maybe call it isEligible -- that takes on the values true/false.
It looks like you know how to calculate the value of isEligible, because you have used it to determine what to display to the screen.

Jorge Briceño
Jorge Briceño 2018 年 1 月 28 日
Hi Alexandra,
Maybe another alternative would be:
function [isEligible]=eligible( v,q )
Your_Output_Name= v>88 && q>88 && ((q+v)/2)>=92
end
Cheers, Jorge
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 1 月 28 日
Your_Output_Name would have to be isEligible for this to work.
Jorge Briceño
Jorge Briceño 2018 年 2 月 5 日
Ok, I had a silly typo. Thanks, Walter.
function [Your_Output_Name]=eligible( v,q )
Your_Output_Name= v>88 && q>88 && ((q+v)/2)>=92
end

サインインしてコメントする。


ledinh lam
ledinh lam 2016 年 11 月 25 日
編集済み: DGM 2023 年 3 月 3 日
I think it will be :
function el=eligible(v,q)
if mean([v,q]) >= 92 && v>88 && q >88
el=true;
else
el=false;
end
end

Duddela Sai Prashanth
Duddela Sai Prashanth 2018 年 9 月 23 日
function [out] = eligible(v, q)
if v > 88 && q > 88 && (v+q)/2 >= 92
out = true;
else
out = false;
end

Yamen Al-Jajan
Yamen Al-Jajan 2019 年 11 月 23 日
function admit=eligible(v,q)
if v>=88 & q>=88
ave=(v+q)/2;
if ave>=92
admit=true;
else
admit=false;
end
else
admit=false;
end

Rahul Krishna
Rahul Krishna 2021 年 5 月 31 日
function admit = eligible(v,q)
if (q +v)/2 >=92 && (v>88 && q>88)
admit = true
fprintf(' the candidate is eligible \n')
else
admit = false
fprintf(' the candidate is not eligible \n')
end

MALK adil
MALK adil 2021 年 12 月 29 日
function out = eligible(v,q)
out = (v+q)./2 >= 92 && v > 88 && q > 88;
if out==1
out= true;
else out= false;
end
  1 件のコメント
Image Analyst
Image Analyst 2021 年 12 月 29 日
編集済み: Image Analyst 2021 年 12 月 29 日
@MALK adil your if block is totally unnecessary.
out is already a boolean (true or false) by how you define it. You do not need to check if it's a 1 (double) and then assign it to true (which it already is). I believe that when you compare a logical to a double (like bln == 7) it converts the logical to a double (false converts to 0, and true converts to 1) and then it compares that double to the number (7) and returns a double.
out = true
out = logical
1
result = out == 7 % Convert out to double then compare the two doubles and return a logical
result = logical
0
And if it's not 1 (the only other choice is false) there is no need to set it to false. It's already false!
So you could simply have done
function out = eligible(v,q)
out = (v+q)./2 >= 92 && v > 88 && q > 88;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by