Shifting and Sorting Matrices

Shift and Sort Functions

Use these functions to shift or sort the elements of a matrix.

Function

Description

circshift

Circularly shift matrix contents.

sort

Sort array elements in ascending or descending order.

sortrows

Sort rows in ascending order.

issorted

Determine if matrix elements are in sorted order.

You can sort matrices, multidimensional arrays, and cell arrays of character vectors 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.

Shifting the Location of Matrix Elements

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 =

  logical

   1                    % Yes
Sorting the Data in Each Column

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 makes a 6-by-7 arbitrary test matrix:

A = floor(gallery('uniformdata',[6 7],0)*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 =
  logical
   1
Sorting the Data in Each Row

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:

A = floor(gallery('uniformdata',[6 7],0)*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 =
  logical
   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.

[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 second row of index contains the sequence, 4 6 3 7 1 5 2, which means that the second row of matrix r is comprised of A(2,4), A(2,6), A(2,3), A(2,7), A(2,1), A(2,5), andA(2,2).

Sorting Row Vectors

The sortrows function sorts the entire row of a matrix according to the elements in a specified column, maintaining the order of elements in each row.

For example, create a random matrix A:

A = floor(gallery('uniformdata',[6 7],0)*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

Use the sortrows function to sort the rows of A in ascending order according to the values in the first column:

r = sortrows(A,1)
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

Now sort the rows of A in ascending order according to the values in the fourth column:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值