MOVENAN (v1.0, mar 2011)

Move NaNs to the beginning or end of a column
253 Downloads
Updated 1 Mar 2011

View License

MOVENAN - Move NaNs to the beginning or the end of a column

For vectors, MOVENAN(V,1) moves the NaNs in V to the end. MOVENAN(V,-1)
moves them to the beginning. For 2D matrices, MOVENAN(M,1/-1) moves the
NaNs in each column of X to the end (1) or the beginning (-1).
MOVENAN(X) is the same as MOVENAN(X,1). The order of the non-NaN
elements in the input is unchanged.

[Y, XI, YJ] = MOVENAN(..) returns indices XI and YJ that can be used to
transform Y into X. For vectors, Y = X(XI) and X = Y(YJ). For matrices,
XI and YJ specify row indices, so that "Y(:,k)=X(XI(:,k),k)" and
"Y(YJ(:,k),k)=X(:,k)", for column k.

Examples:
X = [ 7 4 NaN 4 ;
NaN 3 NaN 3 ;
9 NaN 4 1 ;
10 11 NaN -1 ]
[Y, xi, yj] = movenan(X)
% Y= 7 4 4 4
% 9 3 NaN 3
% 10 11 NaN 1
% NaN NaN NaN -1
% show the relationsship between X and Y according to i and j:
c=2 ; [ Y(:,c) X(xi(:,c),c) ]
c=1 ; [ Y(yj(:,c),c) X(:,c) ]

% using transpose operations we can move along rows:
Y = movenan(X.', -1).'
% NaN 7 4 4
% NaN NaN 3 3
% NaN 9 4 1
% NaN 10 11 -1

See also sort, isnan, nanmean

Inspired by a post on CSSM (http://www.mathworks.com/matlabcentral/newsreader/view_thread/303636). I do not directly see a practical use for it, but, at least in my opinion, it may have an educational value.

Cite As

Jos (10584) (2024). MOVENAN (v1.0, mar 2011) (https://www.mathworks.com/matlabcentral/fileexchange/30584-movenan-v1-0-mar-2011), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
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