1

I'm doing a set of problems from the MATLAB's introductory course at MIT OCW. You can see it here, it's problem number 9, part g.iii.

I have one matrix with the final grades of a course, all of them range from 1 to 5. And I have another array with only letters from 'F' to 'A' (in a 'decreasing' order).

I know how to change elements in a matrix, I suppose I could do something like this for each number:

totalGrades(find(totalGrades==1)) = 'F';
totalGrades(find(totalGrades==2)) = 'E';
totalGrades(find(totalGrades==3)) = 'C';
totalGrades(find(totalGrades==4)) = 'B';
totalGrades(find(totalGrades==5)) = 'A';

But then, what's the purpose of creating the string array "letters"?

I thought about using a loop, but we're supposed to solve the problem without one at that point of the course.

Is there a way? I'll be glad to know. Here's my code for the whole problem, but I got stuck in that last question.

load('classGrades.mat');
disp(namesAndGrades(1:5,1:8));
grades = namesAndGrades(1:15,2:size(namesAndGrades,2));
mean(grades);
meanGrades = nanmean(grades);
meanMatrix = ones(15,1)*meanGrades;
curvedGrades = 3.5*(grades./meanMatrix);

% Verifying 
nanmean(curvedGrades)
mean(curvedGrades)

curvedGrades(curvedGrades>=5) = 5;

totalGrades = nanmean(curvedGrades,2);

letters = 'FDCBA';

Thanks a lot!

4
  • Maybe something like totalGrades(find(totalGrades==1))=letters(1); and then totalGrades(find(totalGrades==2))=letters(2); and so on... Commented Feb 10, 2016 at 12:26
  • or you might want to try something like arrayfun() Commented Feb 10, 2016 at 12:33
  • 1
    This would be a great question to ask your professor. See also: Matrix Indexing Commented Feb 10, 2016 at 12:48
  • It did change something but I don't know if I require some extra line to convert a number to string. Take a look at my outcome: totalGrades(totalGrades==5)=letters(5) y = 4 3 4 4 4 65 3 3 4 3 3 3 3 65 4 Commented Feb 10, 2016 at 13:02

1 Answer 1

1

Try:

letters=['F','D','C','B','A'];
tg = [1 2 1 3 3 1];
letters(tg)

Result:

ans = FDFCCF

This works even when tg (total grade) is a matrix:

letters=['F','D','C','B','A'];
tg = [1 2 1 ; 3 3 1];
result = letters(tg);
result


result =                                                                                                                                                                             

FDF                                                                                                                                                                             
CCF

Edit (brief explanation):
It is easy to understand that when you do letters(2) you get the second element of letters (D).

But you can also select several elements from letters by giving it an array: letters([1 2]) will return the first and second elements (FD).

So, letters(indexesArray) will result in a new array that has the same length of indexesArray. But, this array has to contain numbers from 1 to the length of letters (or an error will pop up).

Sign up to request clarification or add additional context in comments.

3 Comments

It would be useful to explain why this is the case, since it's the part OP is not understanding.
Thanks a lot R. Schifini, nailed it. And as always, it was a pretty simple solution. Need to study more.
You were trying to use logical indexing to change the numbers to letters, but this solution is more a lookup on letters by using total grades as the array of indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.