Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Can't Figure out where I have gone wrong, could use some help please
3 ビュー (過去 30 日間)
古いコメントを表示
here is the question:
How much you can expect to earn the rest of your life depends, among other things, on what level of college degree you have, your gender, and your age when you graduate. Assuming you work full-time until retirement at age 65, your lifetime earnings can be estimated by: LTE = C * (65 – age at graduation), where C is found in the table below and in the file “cTable.mat”:
Using nested switch/case structures, write an m-file function that returns the estimated lifetime earnings of a user. Name your function: LastnameFirstinitial_LTE.m. The function should have 3 inputs. The first input should be the user’s age at graduation in years. The second input should be the user’s gender as a string (either ‘male’ or ‘female’). The third input should be the user’s college degree as a string (either ‘BS’, ‘MS’, or ‘PhD’). If the user provides an invalid string for inputs two or three, ‘improper input’ should be displayed in the command window and the output should be an empty value.
The file “cTable.mat” is a matrix that contains the values for C shown in the above table. In your function, load this file to your workspace and use these values in the calculation of LTE. C must come from this matrix and should not be defined manually within your function.
Note: I have a 3 by 2 , matrix C with values for male/female earnings at each degree.
Here is my code so far, I am brand new to building functions and would love a little boost.
function [LTE] = DeVineyB_LTE(age,gender,degree)
gender = ('male','female')
degree = ('BS','MS','PhD')
switch (gender & degree)
case 'male' & 'BS'
LTE = C(1,1)*(65-age)
case 'male' & 'MS'
LTE = C(2,1)*(65-age)
case 'male' & 'PhD'
LTE = C(3,1)*(65-age)
case 'female' & 'BS'
LTE = C(1,2)*(65-age)
case 'female' & 'MS'
LTE = C(1,3)*(65-age)
case 'female' & 'PhD'
LTE = C(1,1)*(65-age)
Just need a hint or to to help me with this function. Thanks!
1 件のコメント
Image Analyst
2013 年 2 月 4 日
Something's wrong with your C. What does each index of C represent? Why do male BS and female PhD both use C(1,1)? What happened to C(2,2) and C(3,2)? Why are those not being used? You have 6 cases, yet your C seems to be a 3 by 3 matrix instead of a 3 by 2, or 2 by 3. Why???? Please explain C.
回答 (3 件)
Matt Tearle
2013 年 2 月 4 日
It looks like what you're trying to do is index into the correct row and column of C, based on the input strings for gender and degree. One neat way to do that is to compare the given strings with cell arrays of the allowed strings.
For example,
poss_gender = {'male','female'};
makes a 2-element cell array containing the two allowed possibilities (sorry, not very trans-friendly -- don't blame me, I didn't write the problem!). You can use the strcmp (or strcmpi) function to compare a given string to both of these:
whichgender = strcmp(gender,poss_gender)
This will result in a 2-element logical array, with at most one true value. You can use logical functions like any and all, or counting functions like sum and nnz, to check whether there is at least one true as well (for the error-checking part of the problem).
You can use the logical array whichgender to index into any 2-element dimension of another variable, such as C. For example,
C(:,whichgender)
extracts the first column of C if gender is 'male', and the second column if it is 'female'. It will return an empty array if it is neither.
Repeat for degree.
HTH :)
BTW, since I'm here... don't define anything called gender or degree at the beginning of your function, because those names are given to the inputs to the function!
0 件のコメント
Azzi Abdelmalek
2013 年 2 月 4 日
What
gender = ('male','female')
degree = ('BS','MS','PhD')
is doing in your code?
1 件のコメント
Azzi Abdelmalek
2013 年 2 月 4 日
gender='male',degree='PhD'
test=[gender degree]
switch test
case 'maleBS'
LTE = C(1,1)*(65-age)
case 'maleMS'
LTE = C(2,1)*(65-age)
case 'malePhD'
LTE = C(3,1)*(65-age)
end
Image Analyst
2013 年 2 月 4 日
Get rid of the switch and simply do this:
LTE = C(gender, degree)*(65-age);
gender = 1 or 2, for male or female, or whatever. degree = 1, 2, or 3 depending on BS, MS, or Ph.D. or whatever it is.
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!