Simple, generalized shuffle function

Version 1.0.0.0 (1.53 KB) by AJ
A very simple yet very general shuffle that works in a manner similar to other Matlab built-ins.
636 Downloads
Updated 30 Jan 2010

View License

%SHUFFLE Shuffles elements.
% For vectors, SHUFFLE(X) shuffles the elements of X.
% For matrices, SHUFFLE(X) shuffles the rows of X.
% For N-D arrays, SHUFFLE(X) shuffles along the first non-singleton
% dimension of X.
%
% SHUFFLE(X,DIM) shuffles along the dimension DIM. If DIM is 0,
% then all elements of X are shuffled linearly (independently of
% any dimension).
%
% [Y,I] = SHUFFLE(X,DIM) also returns an index vector I.
% If X is a vector, then Y = X(I).
% If X is a matrix and DIM=1, then Y = X(I,:).
%
% Examples:
% X = [1 2 3; 4 5 6; 7 8 9]
% X =
% 1 2 3
% 4 5 6
% 7 8 9
%
% [Y,I] = shuffle(X)
% Y =
% 7 8 9
% 4 5 6
% 1 2 3
% I =
% 3 2 1
%
% [Y,I] = shuffle(X,2)
% Y =
% 1 3 2
% 4 6 5
% 7 9 8
% I =
% 1 3 2
% Created: Ankur Jain (encorejane@gmail.com) - 01/30/2010

Cite As

AJ (2026). Simple, generalized shuffle function (https://www.mathworks.com/matlabcentral/fileexchange/26536-simple-generalized-shuffle-function), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Creating and Concatenating Matrices in Help Center and MATLAB Answers
Tags Add Tags

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