Code covered by the BSD License  

Highlights from
Sort Matrix Elements with Positions

Be the first to rate this file! 4 Downloads (last 30 days) File Size: 1.92 KB File ID: #27232

Sort Matrix Elements with Positions

by Kadir

 

12 Apr 2010 (Updated 07 May 2010)

A function to sort values of the matrix elements, providing their positions in original matrix.

| Watch this File

File Information
Description

SORTMATRIXELEMENTS(A) returns a matrix that is composed of 3 columns:
  first column is the array of the sorted (ascending) values of the
  original matrix second column is the array of corresponding row positions
  and third column is that of column positions.
  
  SORTMATRIXELEMENTS(A,dir) returns sorted values with a descending order
  if dir is equal to -1, or with an ascending order, otherwise.
 
  A : the matrix to be sorted
  dir: direction of the sort, -1 for descending, 1 for ascending (default)
 
  I want to sort the matrix elements and at the same time
  to know the position of each. I will create an array of arrays which
  consists of elements that each has its value, row position and column
  position. An example:
  A = [8 2 6 11; 4 7 9 12; 5 1 3 10];
  Array of arrays (X)
X is [8 1 1;
      2 1 2;
      6 1 3;
     11 1 4;
      4 2 1;
      7 2 2;
      9 2 3;
     12 2 4;
      5 3 1;
      1 3 2;
      3 3 3;
     10 3 4]
And result of the sort (y)
Y is [1 3 2;
      2 1 2;
      3 3 3;
      4 2 1;
      5 3 1;
      6 1 3;
      7 2 2;
      8 1 1;
      9 2 3;
     10 3 4;
     11 1 4;
     12 2 4 ]

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
12 Apr 2010 Matt Fig

There is no help, and no real H1 line in this file. Why not use the faster engine:

[As,Y] = sort(A(:));
[Y,c] = ind2sub(size(A),Y);
Y = [As,Y,c];

12 Apr 2010 Kadir

thanks for the comment! i'm not at good using the built-in functions. but you're right. i will use your code and add help.

15 Apr 2010 Gianni Schena

matlab sort is much faster! ,
one may consider improving the original matlab sort by adding an additional parameter (say k) for ordering at most the first kth largest elements and the first k corresponfing indices

07 Jun 2010 Dinesh Jayaraman

I know it's simple and all, but shouldn't matlab be providing a straight function for something as basic as this? anyway thanks, Matt.
I spent an hour trying to do this very thing, 'cos i didn't know about ind2sub() and then on a hunch, checked here!

07 Jan 2011 Shyamsunder

I am converting an image into a matrix using imread. Then I am taking the R component. After that I am sorting the matrix elements using your program. Here the problem is that for the rows and columns greater than the value 255, the indices are not getting the value i.e. there's no indices such as (300,400) for a value. likewise no index like this one:(400,500) etc.

But if I take ones(512,512). The indices are shown for more than 255.

Please give a solution to this problem.

Please login to add a comment or rating.
Updates
12 Apr 2010

i added h1 line

14 Apr 2010

i changed the example

07 May 2010

I used faster built-in functions

Tag Activity for this File
Tag Applied By Date/Time
sort Kadir 12 Apr 2010 13:22:07
matrix Kadir 12 Apr 2010 13:22:07
positions Kadir 12 Apr 2010 13:22:07

Contact us at files@mathworks.com