File Exchange Pick of the Week

September 11th, 2007

MATLAB Basics video: Sorting matrices

This video covers how to sort a matrix, either sorting each column independently or sorting such that the original rows stay together. Most new users are able to find the SORT command without much problem, but the very useful SORTROWS is often missed.

Find the files here.

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

Other MATLAB Basics posts have been gathered here:
http://blogs.mathworks.com/pick/category/matlab-basics/

9 Responses to “MATLAB Basics video: Sorting matrices”

  1. slaesche replied on :

    Good tip! Just what I was looking for. Thanks.

  2. mayank replied on :

    Hi,I want to sort the coordiantes.How to do this?
    Eg- 1 0
    3 2
    21 0
    4 4
    2 3
    5 4
    so that the sorted coordinates come out to be:
    1 0
    21 0
    3 2
    2 3
    4 4
    5 4

  3. Doug replied on :

    Mayank,

    Please watch the video again. You will see Sortrows at the end. This is the function you are looking for.

    Doug

  4. Sanchay Subhedar replied on :

    I am getting this error on using simple sort function on the same magic square as your.

    ??? Index exceeds matrix dimensions.

    How can I “sort” this problem ;-)

  5. Doug replied on :

    Sanchay,

    You need to show all the relevant code to get help.

    Doug

  6. Renato replied on :

    Hi, is there any function that does the same as sortrows but for columns?

    Thx for your time!

  7. Doug replied on :

    Renato,

    What you need to do is use the transpose operator (’) to transpose the rows to columns. Then you can use sort rows and transpose right back.

    Doug

  8. Mark replied on :

    Hello,

    I need to sort the pixel intensity values of an image in descending order, but then also be able to keep track of each intensities original coordinate in the image. I assume I will need to create an Nx3 matrix where N is the total number of pixels and the 3 column values are: 1. the pixel intensity, 2. the original col, and 3. the row coordinate.

    Could you help me to get started doing this?

    Thanks,
    Mark

  9. Doug replied on :

    @Mark

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html

    Use sort with the second output argument. That gives the absolute index, but you can use IND2SUB to get back to row and column.

    -Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.