SORTBREAK (v1.0, nov 2008)

sort elements of a vector between break points
1.6K Downloads
Updated 5 Nov 2008

View License

Y = SORTBREAK(X,BP) sorts the elements within sections of the vector X
in ascending order. The sections are defined by the breakpoint
indices BP. Each section is sorted independently.

If BP is a single index, the two sections are X(1:BP) and X((BP+1):end).
Example:

Y = sortbreak([5 3 7 1 4 2 8 6],4)
% Y -> [1 3 5 7 2 4 6 8]

If BP contains more indices, the indices are used in sorted order, and
the first section contains the elements X(1:BP(1)), the second
section contains the elements X(BP(1)+1:BP(2)), etc. The last
section contains the elements X(BP(end)+1:end). Example:

Y = sortbreak([5 3 7 1 4 2 8 6],[2 6])
% Y -> [3 5 1 2 4 7 6 8]

SORTBREAK(X,BP,'fixed') excludes the break points from sorting. Example:

% sort between zeros
X = [3 1 2 0 3 1 -4 0 6 4 7 0 5 3] ;
Y = sortbreak(X,find(X==0),'fixed')
% Y -> [1 2 3 0 -4 1 3 0 4 6 7 0 3 5]
% whereas Y = sortbreak(X,find(X==0))
% would yield [0 1 2 3 -4 0 1 3 ...]

[Y,I,J] = SORTBREAK(X,BP) returns sorting indices I and J such that
that Y equals X(I) and X equals Y(J).

Notes:
- Break point indices larger than the number of elements in X are
ignored.
- If BP is empty, the whole vector is sorted.
- For arrays, X is treated as a vector, but Y, I and J will have the
same size as X.
- To sort between NaNs, the third argument 'fixed' is not needed as
NaNs are always sorted at the end of the sublist
X = [3 1 2 NaN 3 1 -4 NaN 6 4 7 NaN 5 3] ;
Y = sortbreak(X,find(isnan(X)))
% Y -> [1 2 3 NaN -4 1 3 NaN 4 6 7 NaN 3 5]

See also : sort, sortrows, issorted
randpermbreak (File Exchange)

Cite As

Jos (10584) (2024). SORTBREAK (v1.0, nov 2008) (https://www.mathworks.com/matlabcentral/fileexchange/22007-sortbreak-v1-0-nov-2008), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Shifting and Sorting Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0