**matlab sort** This is a topic that many people are looking for.

**thevoltreport.com**is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today,

**thevoltreport.com**would like to introduce to you

**Using the Sort and Sortrows Functions in MATLAB**. Following along are instructions in the video below:

All right. So, now were going to look at doing sorting in MATLAB. So sort, you can just do like you would think it would happen. I have this and I want to sort, so “blah= sort (x), and I run that. Its gonna go ahead and sort it, and youre like wow! Not really shocked, but you should be because actually sorting is this huge deal. You can like do graduate work in like sorting algorithms and MATLAB just does it. I dont know actually what method they used to do sorting. If I wanted to know maybe the help can tell me. I dont know if they get that detailed into it, and if they did, how much you would care. It doesnt actually say what method it uses to sort. It just says that it sorts. I bet we could figure it out if we wanted to, but anyway… Sorting! So there you go. So we can sort stuff. We can sort stuff by descending order, and thats a little weird but its a tag, and it has to be in these little quotes because its text. So we sort X by descend, and it actually sorts it in descending order which is kind of fun. Lets see what else can we do thats fun with sorting? Okay. So here weve got ourselves a nice little blah blah blah thing. Lets do this. Alright, so those are just, sorting vectors is pretty straightforward. But whats more fun, and so comment sardine vectors. Well call this sorting matrices because its funner. So weve got this little matrix, not matrixces, matrices. Capital letters. Anyway. So Ive got this matrix. Now if Im just going to sort on a, what do you expect it would do? Well if you remember or know anything about MATLAB, you know that MATLAB sorts things or thinks things by columns. So like whenever we found the max of a matrix, it would find the max of the column, and in fact thats what happens here. Whenever we sort A, if A is a matrix, it actually sorts the first column. So 1, 3, 5, 10, 82. 1, 3, 5, 10, 82, and then 1, 2, 3, 4, 5. So it actually sorts it by column. So … each column. Weve kind of done this in other videos, but say you wanted to sort by row. So What would you do if you wanted to sort by rows? Because you know that- not “sort by rows,” let me say “sort each row.” How about that? Thats really important. Im actually gonna emphasize this. “Note that I did not say sort by rows.” That is actually a totally different meaning. So if I want to say, if I want to sort by each row, or sort each row, so basically the idea would be I wanted to go 1, 3, 2, 10, 1, 3, 4, 82, and 5, 5. Think about it. Think about it. Honestly, pause the video see if you can come up with a good strategy for it. The more you do this, the more youre going to train yourself to think like a programmer. If all youre doing is listening and that kind of passive learning thing, youre not getting as much out of this as you could be. So remember what we need to do is we need to trick MATLAB into thinking that the rows are the columns, and so we can accomplish this by using a transpose. So now I can actually if I transpose it, my rows have become my columns, and columns become my rows. So whenever they even sort the columns, Im actually sorting what? What are, were the rows. Now theoretically, if I do this, that should pop them back around, and it does. So basically, what I did is I; is I said, well transpose it, sort the columns, and then transpose it back, and so now it looks like its all perfectly sorted. So MATLAB doesnt have to have separate functions for sorting rows and sorting columns because we can accomplish that with just some simple transpose… transpose functions. Got it? Awesome. So now, so the reason I said specifically that I did not say sort by rows, is sorting by rows is a totally different kind of process. So if I use whatever. So Im going to call this new section sorting by rows. Okay, so I

like, I like to do that. Alright. So the idea here is you might say, Well Im not really sure what you did. You just added the word sortrows, instead of calling it sort, you call it sort rows. All right, but now look whats happened. You see how this row is [3, 1, 3,]. The row down here is still [3, 1, 3], [1, 9, 3], is still [1, 9, 3], and then [4, 3, 6] is still [4, 3, 6]. Okay and you say, well thats really stupid. Why did it just sort the first one, and it left everything else intact? Your like well, maybe its not that stupid because lets say that this is a table, and the first thing was you know like your name or your ID number, and then the second thing was like your, I dont know. Whats a single digit thing that you have? Your age, and then the next thing in it was your height in feet. Thats… well I guess if youre one, youre not three feet tall. I have no idea. You could call it your number of cats. Okay so this is really not important, and then if I was going to go over here and actually display A, Come here. Then this might actually make a little bit more sense. So trust me just bear with me. Alright, so heres my ID number, my age, and my number of cats. So lets say that I wanted to sort that information by my ID number. So now if I display, Ive resorted this, so person number one still has 9, is 9 years old and have three cats, and person number 3 is still 1 year old, and has 3 cats, and person number 4 still 3 years old, and has 6 cats, and so that information is still preserved. So does that make sense? This is really, really, really important, the sort rows. So basically what sort rows, sortrows preserves the information contained within each row. Specifically the associations within that row. So does that kind of make sense? So I think, I think the labeling makes sense. So basically that whatever the first person is,then I can sort by that ID number, and you say okay well that kind of makes sense, and this is kind of fun. So while were doing this, we can say Im just gonna apparently keep doing this, but, now I can also display, well call it “whatever2. Say I wanted to show it in descending order. So I could go negative one and then display. All right, so this is the same information, but Ive displayed it in descending order. So the -1, youre like well the other one I wrote descend, and I dont think that actually works here. Descend? No that doesnt work. So you cant use descend on sort rows, but you can use -1, but thats actually amazing. Its actually really, really good because that -1, believe it or not, allows us to do other things. So let me display, sorted by ID# in descending order. So there you go. Now you can kind of keep an idea whats going on. So the first one was sorted by ID in ascending order, so there we go. Now you got all kinds of stuff labeled. Thats fun. Its fun to label. All right um, so now lets say- so lets say we no longer wanted sort by your ID number, but we want to sort by your age. In ascending order. So think about what you would do, and in fact this is another wonderful opportunity to pause the video. Try it, so you can get it to make sense. So you can get it to work out. How would you think that youve sort by age in ascending order? Remembering of course that age is the 2nd column. Okay, so age its the second column. Im gonna say this again. Age is the second column. What column is age? Its the second column. Thats right. Alright, so im going to keep my little display. So sort rows, so Im gonna sort A. Im gonna sort it by row. Im actually gonna sort it by the column two, and if youre saying well how come Im seeing blah twice, or Im seeing my sorted value twice? Im not seeing this pretty thing that youve got here. Its probably cuz youre forgetting

put the semicolon there. Remember that the semicolon, um suppresses your input. And so see now, what I got, I have it sorted by age in ascending order. Doo to do too to. So it would not be uncommon for someone to ask you to do something like this; like sort by number of cats in descending order. So again pause the video. See if you can come up with how to sort by the number of cats in descending order. Did you pause it? Did you try it? Again if youre just sitting here like typing exactly what I type, all youre getting is typing practice. You dont need typing practice. If youre trying to program in MATLAB, well maybe you do need typing practice, but go do something else with typing practice. Theres a lot more efficient ways to do typing. This is not an efficient way to do typing practice. So if youre going to sort by the number of cats in descending order, of course thats going to be the third column with a -3 cause its in descending order. And Im going to display. Now check it out. You say well there are two people that have to 3 cats, why does it go with this one first? Thats because that one was listed first. I think. Ill go back to my original data. Yeah. The ID number three person was listed first, so they continued to be listed first. Make sense? Kind of see where were going here? All right, and and thats more of what were doing. So this is just another example, so say these are somebodys grades, and Im gonna- Were gonna make up the same kind of kind of thing. Were gonna say that you have your student ID, and then you have your, you know okay these are test scores, and well call them out of ten. So test 1, test 2 and test 3. All right so Im gonna display grades. CLC CLEAR. All right so I want to make these pretty. There you go. So now like a typical question, I might say something like, “display the grades in order, sorted by ID number, starting with one.” All right. Then display the grades sorted by test 3 in descending order. So its like. I want to know who got the lowest grade on test three, so sort these by test three. Display the grades sorted by test 2, in ascending order. Okay, so now seriously, I know you may or may not be doing this in previous parts of this video, but please pause the video. Go see and make sure that you can get this because if you can put these together, then that means you actually understand whats going on. If you cant put this together that means you have no idea whats going on, or less idea of whats going on. Then you really need to go pursue like getting it and stuff. So pause the video. Go play around with it. See if you can find exactly how to do this. All right so all I can do is beg, and at this point lets assume that youve done so. So answer one, I want to display the grades in order sorted by ID number. So thats just a straight forward sort rows. [grades] There we go. Alright, and I could put labels on this if I wanted to. Answer 2. Sort rows, sorted by test 3 in descending order. Now if youre not paying attention you might say minus 3 because youre thinking its test three, but you need to remember that test three is actually the fourth column, okay? So I kind of tried to be a little tricky, and maybe it wasnt that tricky, but test 3 is in fact not column 3, column three is test 2. So I was really column 4, test 3 is common 4, in descending order. And thats what we have there, and now were gonna sort the grades sorted by test 2 in ascending order I almost did it myself, so test 2 is column 3 in ascending order. Now again, number eight person and then number 4 person and number 1 person are not necessarily in order, and thats because their grades are the same, and in my original lineup I listed 4 first. All right. Get it? Alright if you can do that youre in good shape.

tags:

MATLAB (Programming Language), sort, sortrows

Thank you for watching all the articles on the topic **Using the Sort and Sortrows Functions in MATLAB**. All shares of thevoltreport.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.