issorted - Determine if array is sorted (2024)

Determine if array is sorted

collapse all in page

Syntax

TF = issorted(A)

TF = issorted(A,dim)

TF = issorted(___,direction)

TF = issorted(___,Name,Value)

TF = issorted(A,'rows')

Description

TF = issorted(A) returns logical 1 (true) when the elements of A are in sorted order and logical 0 (false) otherwise.

  • If A is a vector, then issorted returns1 when the vector elements are in ascending order.

  • If A is a matrix, then issorted returns1 when each column of A is in ascending order.

  • If A is a multidimensional array,then issorted returns 1 when A isin ascending order along the first dimension whose size does not equal1.

  • If A is a timetable, then issorted returns1 when its row time vector is in ascending order. To check the orderingof row times or variables of a timetable with additional options,use the issortedrows function.

example

TF = issorted(A,dim) returns1 when A is sorted along dimension dim.For example, if A is a matrix, then issorted(A,2) returns1 when each row of A is in ascending order.

example

TF = issorted(___,direction) returns1 when A is sorted in the order specified by direction forany of the previous syntaxes. For example, issorted(A,'monotonic') returns1 if the elements of A are ascending or descending.

example

TF = issorted(___,Name,Value) specifiesadditional parameters for checking sort order. For example, issorted(A,'ComparisonMethod','abs') checksif A is sorted by magnitude.

example

TF = issorted(A,'rows') returns1 when the elements of the first column of a matrix are sorted. Ifthe first column contains repeated elements, then issorted looksat the ordering of the second column to determine TF.In general, issorted looks to the column immediatelyto the right to determine TF when the current andprevious columns have repeated elements.

  • If A is a timetable, then issorted checks if the row time vector is in ascending order.

  • This syntax is not supported for a matrix of charactervectors.

Note

This syntax is not recommended. Use issortedrows instead.

Examples

collapse all

Sorted Vector

Open Live Script

Create a vector and check if it is sorted in ascending order.

A = [5 12 33 39 78 90 95 107];issorted(A)
ans = logical 1

Sorted Matrix Rows

Create a 5-by-5 matrix and check if each row is sorted in descending order.

A = magic(5)
A = 5×5 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
issorted(A,2,'descend')
ans = logical 0

Sort each row of A in descending order using the sort function, and check that the result has descending rows.

B = sort(A,2,'descend')
B = 5×5 24 17 15 8 1 23 16 14 7 5 22 20 13 6 4 21 19 12 10 3 25 18 11 9 2
issorted(B,2,'descend')
ans = logical 1

2-D Array of Strings

Open Live Script

Create a 2-D array of strings and determine if each column is sorted.

str = ["Horse","Chicken";"cow","Goat"]
str = 2x2 string "Horse" "Chicken" "cow" "Goat" 
issorted(str)
ans = logical 1

Determine if the rows are sorted from left to right.

issorted(str,2)
ans = logical 0

Determine if each row is sorted in descending order from left to right.

issorted(str,2,'descend')
ans = logical 1

Complex Vector with NaN

Open Live Script

Create a vector containing complex numbers and NaN values.

A = [NaN NaN 1+i 1+2i 2+2i 3+i];

Check that the NaN elements are placed first within the vector, and that the remaining elements are sorted by real part.

issorted(A,'MissingPlacement','first','ComparisonMethod','real')
ans = logical 1

Since the third and fourth elements of A have equal real part, issorted checks if the imaginary part of these elements are also sorted.

imag(A(3))
ans = 1
imag(A(4))
ans = 2

Input Arguments

collapse all

AInput array
vector | matrix | multidimensional array | cell array of character vectors | timetable

Input array, specified as a vector, matrix, multidimensionalarray, cell array of character vectors, or timetable.

  • If A contains missing values, suchas NaN, NaT, <undefined>,and missing, then by default, issorted requiresthat they are placed at the end to return 1.

  • If A is complex, then by default, issorted determinessort order by the magnitude of the elements. If there are consecutiveelements with equal magnitude, then issorted alsochecks the phase angle in the interval (-π, π] to breakties.

  • If A is a cell array of charactervectors or a string array, then issorted determinessort order using the code order for the UTF-16 character encodingscheme. The sort is case-sensitive. For more information on sortedcharacter and string arrays, see Sort Order for Character and String Arrays.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | cell | categorical | datetime | duration | timetable

Complex Number Support: Yes

dimDimension to operate along
positive integer scalar

Dimension to operate along, specified as a positive integerscalar. If no value is specified, then the default is the first arraydimension whose size does not equal 1.

Consider a matrix A. issorted(A,1) checksif the data in each column of A is sorted.

issorted - Determine if array is sorted (1)

issorted(A,2) checks if the data in eachrow of A is sorted.

issorted - Determine if array is sorted (2)

dim is not supported for timetable input.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

directionSorting direction
'ascend' (default) | 'descend' | 'monotonic' | 'strictascend' | 'strictdescend' | 'strictmonotonic'

Sorting direction, specified as one of the following:

  • 'ascend' — Checks if datais in ascending order. Data can contain consecutive repeated elements.

  • 'descend' — Checks if datais in descending order. Data can contain consecutive repeated elements.

  • 'monotonic' — Checks ifdata is in descending or ascending order. Data can contain consecutiverepeated elements.

  • 'strictascend' — Checks if data is in strictly ascending order. Data cannot contain duplicate or missing elements.

  • 'strictdescend' — Checks if data is in strictly descending order. Data cannot contain duplicate or missing elements.

  • 'strictmonotonic' — Checks if data is in strictly descending or strictly ascending order. Data cannot contain duplicate or missing elements.

direction is not supported for timetableinput. Use issortedrows instead.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: issorted(A,'MissingPlacement','last')

MissingPlacementPlacement of missing values
'auto' (default) | 'first' | 'last'

Placement of missing values (NaN, NaT, <undefined>,and missing) specified as the comma-separated pairconsisting of 'MissingPlacement' and one of thefollowing:

  • 'auto' — Missing elementsare required to be placed last for ascending order and first for descendingorder to return 1.

  • 'first' — Missing elementsare required to be placed first to return 1.

  • 'last' — Missing elementsare required to be placed last to return 1.

This name-value pair is not supported for timetable input. Use issortedrows instead.

ComparisonMethodElement comparison method
'auto' (default) | 'real' | 'abs'

Element comparison method for numeric input, specified as the comma-separated pair consisting of 'ComparisonMethod' and one of the following:

  • 'auto' — Check if A is sorted by real(A) when A is real, and check if A is sorted by abs(A) when A is complex.

  • 'real' — Check if A is sorted by real(A) when A is real or complex. If A has elements with consecutive equal real parts, then check imag(A) to break ties.

  • 'abs' — Check if A is sorted by abs(A) when A is real or complex. If A has elements with consecutive equal magnitude, then check angle(A) in the interval (-π,π] to break ties.

More About

collapse all

Sort Order for Character and String Arrays

MATLAB® stores characters as Unicode® usingthe UTF-16 character encoding scheme. Character and string arraysare sorted according to the UTF-16 code point order. For the charactersthat are also the ASCII characters, this order means that uppercaseletters come before lowercase letters. Digits and some punctuationalso come before letters.

Extended Capabilities

The issorted function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced before R2006a

See Also

sort | sortrows | issortedrows

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

issorted - Determine if array is sorted (3)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

issorted - Determine if array is sorted (2024)
Top Articles
Onlyqueenkhloe
Swedberg Funeral Home Obituaries Shawano
Evil Dead Movies In Order & Timeline
Hotels Near 6491 Peachtree Industrial Blvd
Busted Newspaper Zapata Tx
Breaded Mushrooms
The Atlanta Constitution from Atlanta, Georgia
How to know if a financial advisor is good?
Craigslist Pet Phoenix
Snarky Tea Net Worth 2022
Milk And Mocha GIFs | GIFDB.com
Sports Clips Plant City
Alejos Hut Henderson Tx
Burn Ban Map Oklahoma
Mflwer
Fraction Button On Ti-84 Plus Ce
Gayla Glenn Harris County Texas Update
Jenna Ortega’s Height, Age, Net Worth & Biography
Company History - Horizon NJ Health
Teekay Vop
Ou Class Nav
Troy Gamefarm Prices
The Eight of Cups Tarot Card Meaning - The Ultimate Guide
Enduring Word John 15
Jersey Shore Subreddit
Keshi with Mac Ayres and Starfall (Rescheduled from 11/1/2024) (POSTPONED) Tickets Thu, Nov 1, 2029 8:00 pm at Pechanga Arena - San Diego in San Diego, CA
Busch Gardens Wait Times
Imagetrend Elite Delaware
Earthy Fuel Crossword
Grandstand 13 Fenway
Www Violationinfo Com Login New Orleans
Tenant Vs. Occupant: Is There Really A Difference Between Them?
4083519708
Oxford Alabama Craigslist
Myql Loan Login
Eastern New Mexico News Obituaries
Überblick zum Barotrauma - Überblick zum Barotrauma - MSD Manual Profi-Ausgabe
Craigslist Putnam Valley Ny
Noaa Marine Weather Forecast By Zone
Bartow Qpublic
Stewartville Star Obituaries
Wasmo Link Telegram
Paul Shelesh
Stoughton Commuter Rail Schedule
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
Poster & 1600 Autocollants créatifs | Activité facile et ludique | Poppik Stickers
Wvu Workday
Autozone Battery Hold Down
Tyrone Dave Chappelle Show Gif
BYU Football: Instant Observations From Blowout Win At Wyoming
Thrift Stores In Burlingame Ca
Kobe Express Bayside Lakes Photos
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6185

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.