0% found this document useful (0 votes)
122 views

Tutorial Shift Sort

This document discusses shifting and sorting in Matlab. It describes how to: 1) Circularly shift elements in a matrix using the circshift function. 2) Sort elements in a matrix in ascending or descending order along columns or rows using the sort function. 3) Check if data is sorted using the issorted function. It also provides examples of importing data tables from Excel and sorting the rows by price either in ascending or descending order.

Uploaded by

Nida Waheed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Tutorial Shift Sort

This document discusses shifting and sorting in Matlab. It describes how to: 1) Circularly shift elements in a matrix using the circshift function. 2) Sort elements in a matrix in ascending or descending order along columns or rows using the sort function. 3) Check if data is sorted using the issorted function. It also provides examples of importing data tables from Excel and sorting the rows by price either in ascending or descending order.

Uploaded by

Nida Waheed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Shifting and Sorting in Matlab

Shifting Elements in Matrix:


Function: circshift Shifts the Elements of a Matrix Circularly
Syntax: N = circshift( M,K ) ; K Specifies Direction

Example:
M = [1:5; 6:10; 11:15] %Creating 3x5 Matrix

N = circshift(M, [row, column]) %circular right shift

To Perform Circular Left Shift use Negative Sign before the dimension. ( circshift( M, -K ))

Sorting Matrix
Sorting helps us in arranging data in either descending or ascending order, which helps
in better data interpretability. Matlab helps us in achieving it by using following functions.
Function: Sort Sorts elements of Matrix
Syntax: sort(matrix, dimension); % dimension: 1 Column , 2Row

Example:
X = [ 49,29,59,89; 19,79,99,39; 66,26,46,96] %Creating 3x4 matrix
Y = sort(X,1) %Sorting column in ascending order
Z = sort(Y,2) %Sorting row in ascending order
To Check if Data is sorted or not
Function: issorted Checks if the data is sorted
Syntax : issorted( X ); % Returns one if Sorted , else
returns zero
Example:
X = [ 99 65 32 11 80 33 ] %Creating an array

N = issorted(X) %Checking if X is sorted

Y = sort(X) %Sorting X in ascending order

Z = issorted(Y) %checking whether Y is sorted


Importing Data Table and Sorting data:
Example: Let us Consider the Table as Shown

Creating Table in Excel


Table1.xls
Spreadsheet.

To Import Data Table from Excel to Matlab

Click on Import Selection


Table will be Imported to Matlab - Check Variable Window
We can Merge tables with different variables by using functions such as
Join Or by using Concatenation Function : cat,horzcat,vertcat

For Example Consider another Table , as Shown below

Table2.xls

Import Table2 into Matlab


Table3 = join(Table1 , Table2); % Joining table2 with table1
To sort Rows:
Low_to_High = sortrows(Table3,'Price') %Sorting rows in ascending order
%based on Pricing

To Sort the Data in Descending Order


To Sort data in Descending order we need to mention the direction as shown
high_to_low = sortrows(Table3,'Price','descend'); %Sorting rows in ascending

%order based on Pricing

You might also like