function using if or switch statement

4 views (last 30 days)
I am going to make a function called XYZ that takes as its only input argument one positive integer specifying the year of birth of a person and returns as its only output argument the name of the generation that the person is part of ('X', 'Y', or 'Z ') according to the table below. For births before 1966, return 'O' for Old and for births after 2012, return 'K' for Kid . Remember that to assign a letter to a variable, you need to put it in single quotes,
I have make that code
function born=generationXYZ(n)
if 1966<=n<=1980
born='X';
end
if 1981<=n<=1999
born='Y';
end
if 2000<=n<=2012
born='Z';
end
if n<=1966
born='O';
end
if n=>2012
born='K';
end
end
but when i test this code i getting an error when i remove last two if check i always recipe z in output.
I know it can make with switch statement kindly refer me where i need corrections. Thanks in advance for assistance....

Accepted Answer

Guillaume
Guillaume on 20 May 2015
Edited: Guillaume on 20 May 2015
To fix your code, use Walter's answer.
You call also altogether avoid if or switch and use:
function born = generationXYZ(n)
bins = [-Inf, 1966, 1981, 2000, 2013, +Inf]; %as Walter pointed out some of your edges are ambiguous
gennames = 'OXYZK#'; %return # if input is +Inf
born = gennames(discretize(n, bins));
end
This also has the advantage that it also works for vectors
  4 Comments
Guillaume
Guillaume on 20 May 2015
On older versions (with histcounts):
[~, ~, idx] = histcounts(n, bins);
born = gennames(idx);
On even older versions (with histc):
[~, idx] = histc(n, bins);
born = gennames(idx);
Guillaume
Guillaume on 20 May 2015
@muhammad, your comment is confusing, was it addressed to me?
If so, and you want me to elaborate. it's simple, you create bins to partitionate the date and ask matlab with discretize (or histcounts or histc on older versions) to tell you in which bin the date fall. You then use the bin index to extract the correct generation letter.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 20 May 2015
The expression 1966<=n<=1980 is interpreted as ((1966<=n)<=1980) . The first subpart of that, (1966<=n), returns a logical value, 0 for false and 1 for true. That value is then tested against <= 1980 and since 0 and 1 are both <= 1980, the result is always true.
You need 1966 <= n && n <= 1980
You also need to look more carefully at how you are treating the exact year 1966 and how you are treating the exact year 2012
  4 Comments
Walter Roberson
Walter Roberson on 20 May 2015
if 2012 > n then n < 2012 so that test is checking for dates before 2012.
Note: there is no reason to keep checking 1966. Test 1966 first, and if that is false then you know the value must be at least 1966 and need not test against 1966 again.
Muhammad Usman Saleem
Muhammad Usman Saleem on 20 May 2015
Walter Roberson thanks for contribution. i do not get your answer.Try to answer in the form of algorithm..thanks

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!