Code covered by the BSD License  

Highlights from
shuffle_orderby

4.5

4.5 | 13 ratings Rate this file 19 Downloads (last 30 days) File Size: 2.72 KB File ID: #3029

shuffle_orderby

by

 

17 Feb 2003 (Updated )

Two functions: shuffles vectors or matrices, orders according to a predefined order.

| Watch this File

File Information
Description

SHUFFLE Shuffles vectors or matrices.
% SHUFFLE(X) shuffles the elements of a vector or matrix X.
%
% SHUFFLE(X,DIM) shuffles along the dimension DIM.
%
% [Y,I] = SHUFFLE(X) also returns an index matrix I. If X is
% a vector, then Y = X(I). If X is an m-by-n matrix, then
% for j = 1:n, Y(:,j) = X(I(:,j),j); end
%
% Input arguments:
% X - the vector or matrix to shuffle (array)
% DIM - the dimension along which to shuffle (integer)
% Output arguments:
% Y - the vector or matrix with the elements shuffled (array)
% I - the index matrix with the shuffle order (array)
%
% Examples:
% X = [10 25 30 40]
% [Y,I] = SHUFFLE(X)
% Y = 30 25 10 40
% I = 3 2 1 4
%
% X = [10 25 ; 3.2 4.1 ; 102 600]
% [Y,I] = SHUFFLE(X)
% Y = 3.2000 600.0000
% 10.0000 25.0000
% 102.0000 4.1000
% I = 2 3
% 1 1
% 3 2
%
% X = [10 25 50 ; 3.2 4.1 5.5 ; 102 600 455 ; 0.03 0.34 0.01]
% DIM = 1
% [Y,I] = SHUFFLE(X,DIM)
% Y = 10.0000 25.0000 50.0000
% 0.0300 0.3400 0.0100
% 102.0000 600.0000 455.0000
% 3.2000 4.1000 5.5000
% I = 1 4 3 2
%
% X = [10 25 50 ; 3.2 4.1 5.5 ; 102 600 455 ; 0.03 0.34 0.01]
% DIM = 2
% [Y,I] = SHUFFLE(X,DIM)
% Y = 10.0000 50.0000 25.0000
% 3.2000 5.5000 4.1000
% 102.0000 455.0000 600.0000
% 0.0300 0.0100 0.3400
% I = 1 3 2
%
% See also ORDERBY
%
% Created: Sara Silva (sara@itqb.unl.pt) - 2002.11.02

ORDERBY Orders vectors and matrices according to a predefined order.
% ORDERBY(X,I) orders the elements of a vector or matrix X
% according to the index matrix I.
%
% ORDERBY(X,I,DIM) orders along the dimension DIM.
%
% If X is a vector, then Y = X(I). If X is an m-by-n matrix, then
% for j = 1:n, Y(:,j) = X(I(:,j),j); end
%
% Input arguments:
% X - the vector or matrix to order (array)
% I - the index matrix with the ordering to apply (array)
% DIM - the dimension along which to order (integer)
% Output arguments:
% Y - the ordered vector or matrix (array)
%
% Examples:
% X = [10 25 30 40]
% I = [3 2 1 4]
% Y = ORDERBY(X,I)
% Y = 30 25 10 40
%
% X = [10 25 ; 3.2 4.1 ; 102 600]
% I = [2 3 ; 1 1 ; 3 2]
% Y = ORDERBY(X,I)
% Y = 3.2000 600.0000
% 10.0000 25.0000
% 102.0000 4.1000
%
% X = [10 25 50 ; 3.2 4.1 5.5 ; 102 600 455 ; 0.03 0.34 0.01]
% I = [1 4 3 2]
% DIM = 1
% Y = ORDERBY(X,I,DIM)
% Y = 10.0000 25.0000 50.0000
% 0.0300 0.3400 0.0100
% 102.0000 600.0000 455.0000
% 3.2000 4.1000 5.5000
%
% X = [10 25 50 ; 3.2 4.1 5.5 ; 102 600 455 ; 0.03 0.34 0.01]
% I = [1 3 2]
% DIM = 2
% Y = ORDERBY(X,I,DIM)
% Y = 10.0000 50.0000 25.0000
% 3.2000 5.5000 4.1000
% 102.0000 455.0000 600.0000
% 0.0300 0.0100 0.3400
%
% See also SHUFFLE
%
% Created: Sara Silva (sara@itqb.unl.pt) - 2002.11.02

Acknowledgements

This file inspired Knm Cluster.

MATLAB release MATLAB 5.2 (R10)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
05 Dec 2008 Alex Rogers

######### WARNING #############

shuffle.m resets the random number generator using rand('state',sum(100*clock)) each time it is called.

If you call it repeatedly within a short loop it will continually reset the random number generator seed to the same value and return the same shuffle (and also affect every other random number call within your code). If you are trying to average over stochastic effects this could have disastrous affects.

04 Dec 2008 Alex Rogers  
07 Aug 2008 Eve Devaliere

exactly what I was looking for (shuffle) thank you so much!

25 Apr 2008 Pranay Das

thank you very much. this is what I was looking for

17 Dec 2007 doruk bozdag  
11 Dec 2007 A Fasih

I needed to have shuffle operate on rows of a matrix, shuffling each row differently, so I changed the last switchcase to

case 1
[ans,myorder]=sort(rand(size(x)),d);
bar = repmat((0:size(x,2)-1)*size(x,1),size(x,1),1)+myorder;
s=x(bar);
case 2
[ans,myorder]=sort(rand(size(x)),d);
bar=repmat((0:size(x,1)-1)'*size(x,2),1,size(x,2))+myorder;
xt=x';
bar=bar';
s=xt(bar)';

11 Oct 2007 James Porter

For the 1d case this seems to be more reliable. It should be relatively easy to generalise this.

function [y] = shuffle_better(x)
n = length(x);
r = randperm(n);
for i = 1:n
y(r(i)) = x(i);
end
end

28 Aug 2007 Hector M

Thanks very much!

04 Oct 2006 Fidimahery ANDRIANASY  
05 Oct 2005 Bijan Nemati

Shuffle works very efficiently - very handy.

16 Feb 2005 Matthew Salganik

useful and easy -- very helpful

10 Feb 2005 Kaptan Teotrakool

Very efficient. Love it.

01 Feb 2005 Johannes Sarnthein

works nicely

17 Jun 2004 Nathan Funk

Just a note to those that want to shuffle a [1...n] array: there is a built-in function called randperm that will do this for you :)

Nathan

29 May 2004 edgarin leon

good job!

Contact us