简体   繁体   中英

How to replace specific elements of a vector with elements of a string vector?

I've just started to learn MATLAB (and programming for all that matters). I have been able to do some exercises about scalar, vector, and matrix operations, indexing, plotting and scripts. Of course, I'm just trying very basic level exercises. I ran into this one and haven't been able to solve it:

I have a 17x1 column vector made of integers between 1 and 5, which represent the mean grades of a group of students. I have to replace the numbers with the letters F, D, C, B, A so that the grades are now in letters. Now, I could use a very simple approach:

    grades(grades == 1) = 'F'
    grades(grades == 2) = 'D'

And so on, but I'm almost sure that this isn't the most efficient approach, nor the intended by the course (I'm using one of the open courses of the MIT). Specially since one of the hints is to create a string vector:

    letters = 'FDCBA'

Also, this replaces the 1 to 5 integers with 65 to 70 integers (instead of the letter). So, my question is, is there a concise way to do this? I know that in this particular case, it's not really that important whether or not I have a couple of extra lines of code, but still, I'd like to understand what's the role of the vector letters in this exercise. Maybe:

    grades(grades == 1) = letters(1)

Or use a loop and an auxiliary variable to replace elements of grades equal to 'k' with the kth element of letters . Though the first lecture didn't mention loops, so I guess there's a concise way to do this using basic indexing functions and vector operations. I apologize in advance, I know this is probably a very basic question.

As you have already shown a lot of effort I will try to clear some things up for you. The replacement of 1 to 5 with 65 to 70 is due to the ASCII table which represents all characters with integers. As you have an integer array MATLAB will convert the characters with its numerical ASCII value. You can invert this with char(65:70) = ABCDEF;

Also you can index the position of grades multiple times. char_grades =letters(grades) will do the trick. Or to do it in a convoluted way.

g = @(x) letters(x);
char_grades = g(grades);

As you are probably not yet familar with function handles I might explain. I defined a function g which looks up the char value of a grade in letters and applied it to the grades vector. This is essentially the same as above but might help understanding the indexing logic.

To replace the numbers in grades directly with letters the easiest way is:

grades = [ 1 2 3 5 4 3 2 4 5 4 3 5 4] % Numeric grades
letters = 'FDCBA'    
grades = letters(grades) % Line 3

This uses grades as the index to the characters in the vector letters. Assuming that F corresponds to 1 ... A corresponds to 5.
This is not particular good practice, as it changes grades from a numeric vector to a char vector. Preferably save the result in a new variable by replacing code line 3 with

grades_char = letters(grades) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM