I need help with a matrix input and a vector output! please !!

Hello ! I had written all the question here, but for some reasom my computer shut Down.. and i did not save it, so i have attached a Picture of the description instead of writing it down Again. Please see the attached image! : "help matlab.jpg"
I wrote a code as follows:
%(EXAMPLE: a Matrix, lets call it S, like:
| Studentnumber| Name | grade1 | grade2 | grade3 | grade4 |
| S1 | JACK | 2 | 2 | 4 | 7 |
| S2 | RYAN | 10 | 7 | 10 | 7 |
| S3 | PETER| 7 | 7 | 4 | 7 |
| S4 | HARRY| 12 | 10 | 10 | 12 |
| S5 | WILL | 2 | 0 | -3 | 4 |
| S6 | LEIF | 10 | 7 | 4 | 7 |
%For each condition i will use an IF/else function.
If s(:,i) = 1;
finalValue = s(:,i,1);
% for the second condition: ( first the lowest element is removed by MIN
% function and then the average of the rest of the numbers is calculated by MEAN.
elseif s(:,i) > 1
f = min(s(:,i) - s(:,i)
final value = mean(f)
% for the last condition if any of the grades = -3, the final value = -3.
elseif s(:,i)==-3
finalValue = -3.
end
end
(I also thought about using a for loop to run through each row of the matrix, but when i do so the names get "used" to and matlab displays an error. if i for instance use a csv file containing only the numbers then it will Work with a for-loop. But since i need to used csv file with the studentnumber, and names, it won't Work)
The final output should be a vector with length N, containing the final value for each student. Can someone out there please help me out with this ? I will really appreciate it!
Also the values of the grades can only be those of the 7- step - scale: [-3, 0, 2, 4, 7, 10, 12] And the purpose of the script is to run a csv file containing the matrix and then evaluate the answers.
Thanks for your time !

9 Comments

dpb
dpb on 19 Jan 2015
Edited: dpb on 19 Jan 2015
What's the first if condition supposed to represent?
Does the condition of -3 --> -3 apply before or after the rejection of the minimum? Or, in other words, do you throw out the worst and then grade on the remaining?
Have you been able read the data in and can you select the grade values into an array by using cell array syntax? Once we get there, the answer should be able to be written in just a couple of conditional assignments.
"What's the first if condition supposed to represent?" &nbsp That's a trace of an effort to markup the text. @dpb, I guess you seen that before. Now, I removed it.
Thanks per isakson.
The condition for -3 does apply before the rejection of minimum. So you could actually say that the lowest possible element to remove would be 0.
as for the data load, i does not Work for me because of the names in the file.. So as a start i am simply using only using a matrix: [0,2,7,7,12;2,4,10,10,7] but my code does not Work at all..
I have worked on it without Progress for hours, i will continue now :) Thanks for your time Guys!
"So as a start i am simply using only using a matrix:"
>> [0,2,7,7,12;2,4,10,10,7]
ans =
0 2 7 7 12
2 4 10 10 7
Assumption: All student have received a grade for each assignment. Is that true?
ooooh, this is coursework...can't just provide a solution then.
The assignment itself for this function is expecting an array of NxM so your problem of input is to be solved elsewhere.
I was asking about the code
If s(:,i) = 1;
finalValue = s(:,i,1);
as the first if condition; now I see from the assignment you're trying to deal with condition 1. above. It is, however, misguided approach to the problem. (Not to mention for an if the "is equal to" operator is == not the assignment =.)
HINTS:
1. Start by determining how big is the input array (particularly you need to know how many scores; the number of students is really irrelevant)?
2. If it's only the one column your job is done; return that as the output and return.
3. Now you've only the other two cases to take care of.
4. Next, you've got to find out whether condition 3) is true or not for each student as that affects what happens about condition 2)
5. After that, computing the means and then rounding is relatively straightforward. (Remember, though, to do the fixup for the special "-3" case.)
I'll note there's no need for a loop in any of this--in fact, it makes it harder rather than simpler given "how Matlab works".
ADDENDUM
On HINT 4 above, the "straightforwardness" is a little bit deceiving perhaps. I was thinking of a certain Matlab function that will return the desired result in one function call--the question then is what the constraints are upon what you can use. It's a little more logic to determine that if you can't use some of these functions or haven't yet enough familiarity with Matlab that you can uncover the ways in which functions are used on your own simply because haven't had enough "time in grade" to get a handle on what is there...
To try to find it without giving it away entirely (this still is homework, after all; isn't fair to your compatriots if you get a free ride and they don't :) ), think what it is you're trying to end up with--the closest match to one of the allowed values of the average score. There are a couple of ways one can think of doing this, the way I'm thinking of has something to do with a table lookup idea...
I just implemented a couple of functions that return the requested values for the sample dataset. It uses no loops and has about ten code lines in the first function while the rounding function is two lines including the line defining the allowable levels array. Not having the specification for this function, I made the assumption it expects a single column of average grades and only does the rounding so the averaging was done first...altho that can be folded into the one computational line as well if wanted.
Thanks, i will look at and use your hints, thanks for your time !
Hey Again! My code now Works for the first to conditions, but i cannot make it Work for the last one, can someone please have a look at it and help me?
% My code:
[N M] = size(grades);
i = 1:N;
% The logical vector to include the data:
includedData = true(size(grades, 1), 1);
% The three ways to calculate the final grade:
if M == 1
includedData = grades(i,M);
elseif M > 1
includedData = (sum(grades')-min(grades'))/(M-1);
elseif M == -3
includedData = -3;
end
% Display of the Final grade:
gradesFinal = (includedData');
end
Also : couldn't i just remove the "gradesFinal=includedData', and just write gradesFinal instead of includedData under each condition?
Thanks!
Thanks dpb, i will look at it again. To be honest i don't understand, but i will read it through again :) .. Yes, i have only 10 days experience of matlab from basic "programming". I really appreciate your time and help! Thanks!!

Sign in to comment.

Answers (0)

Categories

Asked:

on 19 Jan 2015

Commented:

on 20 Jan 2015

Community Treasure Hunt

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

Start Hunting!