• Support PF! Buy your school textbooks, materials and every day products Here!

User Defined Functions in MATLAB

  • Thread starter samuel1994
  • Start date
  • #1

Homework Statement



Write a user-defined function that calculates grade point average (GPA) on a scale of 0 to 4, where A = 4, B = 3, C = 3, D = 1, and E = 0. For the function name and arguments use av = GPA(g,h). The input argument g is a vector whose elements are letter grades A, B, C, D, or E entered as strings. The input argument h is a vector with the corresponding credit hours. The output argument av is the calculated GPA. Use the function to calculate the GPA for a student with the following record:

Grade B A C E A B D B
Credit Hours 3 4 3 4 3 4 3 2

For this case the input arguments are: g=[‘BACEABDB’] and h=[3 4 3 4 3 4 3 2].


Homework Equations



None.

The Attempt at a Solution



I will be honest here, I have not attempted a solution past the point of defining a function with the output av, called the function GPA and with input arguments g and h. This is because I'm not entirely sure how to go from the string that would be inputted into numbers that should be in the GPA function. For example, I understand that B should be equivalent to 3, A should be 4, etc. However, how do I go from the string to numbers? I have tried defining each of the variables, A, B, C, D, and E after the function is defined, however I get errors if I do this.
 

Answers and Replies

  • #2
Student100
Education Advisor
Gold Member
1,649
416
Post whatever code you've already done so I can take a look at where you're going with it.
 
  • #3
function av = GPA(g,h)
A = 4; B = 3; C = 3; D = 1; E = 0;
av = sum(g)/sum(h)

This is what I have. I know it's wrong, but I'm merely lost on how to convert the string into numbers.
 
  • #4
Student100
Education Advisor
Gold Member
1,649
416
Have you tried using str2num?

A = str2num ('4')
 
  • #5
33,496
5,188
function av = GPA(g,h)
A = 4; B = 3; C = 3; D = 1; E = 0;
av = sum(g)/sum(h)

This is what I have. I know it's wrong, but I'm merely lost on how to convert the string into numbers.
The input arguments g and h are vectors, or arrays. You need to look for some examples of how to get or set the j-th element of a vector.

You also need to look for some examples that show how to use an if ... else if structure. This is the most obvious way of taking a character value in the g array and converting it to a number. There are other, shorter ways, but they're not as obvious.

How do you find out what you have available to work with? Do you have a textbook that explains matlab syntax, or do you use notes from the instructor? The company that markets matlab has a nice website with all sorts of documentation on how to use their product. Here's a link to a page with lots of links to tutorials and documentation - http://www.mathworks.com/academia/student_center/tutorials/launchpad.html.
 
  • #6
33,496
5,188
Have you tried using str2num?

A = str2num ('4')
I don't see how this would be helpful. The OP has an input vector of characters such as 'A', 'B' and the like. All that your example would do is store the number 4 in the A variable. It wouldn't work if used to convert, say 'B'.
 
  • #7
Student100
Education Advisor
Gold Member
1,649
416
I don't see how this would be helpful. The OP has an input vector of characters such as 'A', 'B' and the like. All that your example would do is store the number 4 in the A variable. It wouldn't work if used to convert, say 'B'.
Yeah, you’re absolutely right.
 
  • #8
Thank you for you help. I figured out the problem. This was my final code:

function av = GPA(g,h)
for k = 1:length(g)
if g(k) == 'A'
g(k) = 4;
elseif g(k) == 'B'
g(k) = 3;
elseif g(k) == 'C'
g(k) = 2;
elseif g(k) == 'D'
g(k) = 1;
elseif g(k) == 'E'
g(k) = 0;
end
end
av = sum((g.*h))/sum(h)
 
  • #9
33,496
5,188
If you put a [code] tag at the start and a [/code] tag at the end, it will preserve your indentation, making your code a little easier to read. I've done this below.
Thank you for you help. I figured out the problem. This was my final code:
Code:
function av = GPA(g,h)
for k = 1:length(g)
    if g(k) == 'A'
        g(k) = 4;
    elseif g(k) == 'B'
        g(k) = 3;
    elseif g(k) == 'C'
        g(k) = 2;
    elseif g(k) == 'D'
        g(k) = 1;
    elseif g(k) == 'E'
        g(k) = 0;
    end
end
av = sum((g.*h))/sum(h)
This looks about right. You can verify that it's working correctly by doing the GPA calculation manually and comparing that with the result from your code. If both are the same, that's a good sign.
 

Related Threads on User Defined Functions in MATLAB

  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
5
Views
2K
Replies
3
Views
3K
Replies
11
Views
1K
Replies
1
Views
4K
Replies
25
Views
4K
Replies
2
Views
1K
Replies
1
Views
2K
  • Last Post
Replies
20
Views
2K
Top