Code covered by the BSD License

### Highlights from ismonotonic

5.0
5.0 | 6 ratings Rate this file 35 Downloads (last 30 days) File Size: 2 KB File ID: #11637 Version: 1.2

# ismonotonic

### Richie Cotton (view profile)

05 Jul 2006 (Updated )

Determines whether or not a vector or matrix is monotonically increasing or decreasing.

### Editor's Notes:

This file was a File Exchange Pick of the Week

File Information
Description

ISMONOTONIC(X) returns a boolean value indicating whether or not a vector is monotonic.
By default, ISMONOTONIC returns true for non-strictly monotonic vectors,
and both monotonic increasing and monotonic decreasing vectors. For
matrices and N-D arrays, ISMONOTONIC returns a value for each column in
X.

ISMONOTONIC(X, 1) works as above, but only returns true when X is
strictly monotonically increasing, or strictly monotonically decreasing.

ISMONOTONIC(X, 0) works as ISMONOTONIC(X).

ISMONOTONIC(X, [], 'INCREASING') works as above, but returns true only
when X is monotonically increasing.

ISMONOTONIC(X, [], 'DECREASING') works as above, but returns true only
when X is monotonically decreasing.

ISMONOTONIC(X, [], 'EITHER') works as ISMONOTONIC(X, []).

ISMONOTONIC(X, [], [], DIM) works as above, but along dimension DIM.

NOTE: Third input variable is case insensitive, and partial matching is
used, so 'd' would be recognised as 'DECREASING' etc..

EXAMPLE:
x = [1:4; 6:-2:2 3]
ismonotonic(x)
ismonotonic(x, [], 'i')
ismonotonic(x, [], [], 2)

x =
1 2 3 4
6 4 2 3
ans =
1 1 1 1
ans =
1 1 0 0
ans =
1
0

NEW FEATURES in v1.1:
Code now fully vectorised.
Support for N-D arrays.
Character arrays now allowed.

ACKNOWLEDGEMENTS:
Thanks to Jos X for advice on vectorisation

MATLAB release MATLAB 7.1.0 (R14SP3)
02 Jun 2015 Stephan

### Stephan (view profile)

The matlab function issorted does the job in many cases, but doesn't include the strictly monotonic option, so I prefer to use Richie's function.

17 Oct 2013 Mark Easton

### Mark Easton (view profile)

Giving only a single boolean result for vectors instead of a vector of booleans.

Comment only
11 Oct 2011 Brennan Smith

### Brennan Smith (view profile)

Thank you!

23 Jun 2011 Jeff Jackson

### Jeff Jackson (view profile)

Thanks for the contribution Richie.

It works as expected and replaces one I had been using that did not.

Jeff

GIVEN HOW MANY STANDARD MATLAB FUNCTIONS REQUIRE A VECTOR OF MONOTONICALLY INCREASING/DECREASING VALUES, A FUNCTION LIKE THIS ONE SHOULD BE INCLUDED WITH MATLAB.

FRUSTRATED,

Comment only
18 Feb 2010 cre Esposito

### cre Esposito (view profile)

12 Oct 2009 Jan Simon

### Jan Simon (view profile)

H1 line, complete help, examples, author and date mentioned in the source, fast and working!

For small problems the string recognition takes more time than the calculations:
strmatch(lower(direction), 'either')
Much faster:
strncmpi(direction, 'either', length(direction))
or even (1st letter is enough, handles empty strings):
strncmpi(direction, 'e', 1)

07 Feb 2008 j palter

The function works and works quickly. Saved me some time coding up something clunky to do the same thing. Thanks!