| MATLAB® | ![]() |
| On this page… |
|---|
Shifting the Location of Matrix Elements |
Use these functions to shift or sort the elements of a matrix.
Function | Description |
|---|---|
Circularly shift matrix contents. | |
Sort array elements in ascending or descending order. | |
Sort rows in ascending order. | |
Determine if matrix elements are in sorted order. |
You can sort matrices, multidimensional arrays, and cell arrays of strings along any dimension and in ascending or descending order of the elements. The sort functions also return an optional array of indices showing the order in which elements were rearranged during the sorting operation.
The circshift function shifts the elements of a matrix in a circular manner along one or more dimensions. Rows or columns that are shifted out of the matrix circulate back into the opposite end. For example, shifting a 4-by-7 matrix one place to the left moves the elements in columns 2 through 7 to columns 1 through 6, and moves column 1 to column 7.
Create a 5-by-8 matrix named A and shift it to the right along the second (horizontal) dimension by three places. (You would use [0, -3] to shift to the left by three places):
A = [1:8; 11:18; 21:28; 31:38; 41:48]
A =
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18
21 22 23 24 25 26 27 28
31 32 33 34 35 36 37 38
41 42 43 44 45 46 47 48
B = circshift(A, [0, 3])
B =
6 7 8 1 2 3 4 5
16 17 18 11 12 13 14 15
26 27 28 21 22 23 24 25
36 37 38 31 32 33 34 35
46 47 48 41 42 43 44 45
Now take A and shift it along both dimensions: three columns to the right and two rows up:
A = [1:8; 11:18; 21:28; 31:38; 41:48];
B = circshift(A, [-2, 3])
B =
26 27 28 21 22 23 24 25
36 37 38 31 32 33 34 35
46 47 48 41 42 43 44 45
6 7 8 1 2 3 4 5
16 17 18 11 12 13 14 15
Since circshift circulates shifted rows and columns around to the other end of a matrix, shifting by the exact size of A returns all rows and columns to their original location:
B = circshift(A, size(A));
all(B(:) == A(:)) % Do all elements of B equal A?
ans =
1 % Yes
The sort function sorts matrix elements along a specified dimension. The syntax for the function is
sort(matrix, dimension)
To sort the columns of a matrix, specify 1 as the dimension argument. To sort along rows, specify dimension as 2.
This example first constructs a 6-by-7 random matrix:
rand('state', 0); % Initialize random number generator
A = floor(rand(6,7) * 100);
A =
95 45 92 41 13 1 84
23 1 73 89 20 74 52
60 82 17 5 19 44 20
48 44 40 35 60 93 67
89 61 93 81 27 46 83
76 79 91 0 19 41 1
Sort each column of A in ascending order:
c = sort(A, 1)
c =
23 1 17 0 13 1 1
48 44 40 5 19 41 20
60 45 73 35 19 44 52
76 61 91 41 20 46 67
89 79 92 81 27 74 83
95 82 93 89 60 93 84
issorted(c(:, 1))
ans =
1
Use issorted to sort data in each row. Using the example above, if you sort each row of A in descending order, issorted tests for an ascending sequence. You can flip the vector to test for a sorted descending sequence:
rand('state', 0); A = floor(rand(6,7) * 100);
r = sort(A, 2, 'descend')
r =
95 92 84 45 41 13 1
89 74 73 52 23 20 1
82 60 44 20 19 17 5
93 67 60 48 44 40 35
93 89 83 81 61 46 27
91 79 76 41 19 1 0
issorted(fliplr(r(1, :)))
ans =
1
When you specify a second output, sort returns the indices of the original matrix A positioned in the order they appear in the output matrix. In this next example, the second row of index contains the sequence 4 3 2 5 1, which means that the sorted elements in output matrix r were taken from A(2,4), A(2,3), A(2,2), A(2,5), and A(2,1):
[r index] = sort(A, 2, 'descend');
index
index =
1 3 7 2 4 5 6
4 6 3 7 1 5 2
2 1 6 7 5 3 4
6 7 5 1 2 3 4
3 1 7 4 2 6 5
3 2 1 6 5 7 4
The sortrows function keeps the elements of each row in its original order, but sorts the entire row of vectors according to the order of the elements in the specified column.
The next example creates a random matrix A:
rand('state', 0); % Initialize random number generator
A = floor(rand(6,7) * 100);
A =
95 45 92 41 13 1 84
23 1 73 89 20 74 52
60 82 17 5 19 44 20
48 44 40 35 60 93 67
89 61 93 81 27 46 83
76 79 91 0 19 41 1
To sort in ascending order based on the values in column 1, you can call sortrows with just the one input argument:
sortrows(A)
r =
23 1 73 89 20 74 52
48 44 40 35 60 93 67
60 82 17 5 19 44 20
76 79 91 0 19 41 1
89 61 93 81 27 46 83
95 45 92 41 13 1 84
To base the sort on a column other than the first, call sortrows with a second input argument that indicates the column number, column 4 in this case:
r = sortrows(A, 4)
r =
76 79 91 0 19 41 1
60 82 17 5 19 44 20
48 44 40 35 60 93 67
95 45 92 41 13 1 84
89 61 93 81 27 46 83
23 1 73 89 20 74 52
![]() | Resizing and Reshaping Matrices | Operating on Diagonal Matrices | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |