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.
|
| Updates |
| 12 Apr 2010 |
i added h1 line |
| 14 Apr 2010 |
i changed the example |
| 07 May 2010 |
I used faster built-in functions |
|
Contact us at files@mathworks.com