Code covered by the BSD License  

Highlights from
RANDSWAP

4.25

4.2 | 4 ratings Rate this file 22 Downloads (last 30 days) File Size: 2.32 KB File ID: #12621

RANDSWAP

by Jos (10584)

 

12 Oct 2006 (Updated 26 Nov 2007)

randomly swap elements of a matrix

| Watch this File

File Information
Description

For vectors, Y = RANDSWAP(X) randomly swaps the elements of X. For N-D matrices, RANDSWAP(X) randomly swaps the elements along the first non-singleton dimension of X.

RANDSWAP(X,DIM) randomly swaps the elements along the dimension DIM. For instance, RANDSWAP(X,1) randomly interchanges the rows of X.

Y = RANDSWAP(X,'partial') swaps the elements for each of the non-singleton dimensions of X separately. Rows are interchanged first, then columns, then planes, etc. In this case, elements that belong to the same row, column, ... stay together.
 
Y = RANDSWAP(X,'full') randomly swaps all the elements in X.

[Y, I, J] = RANDSWAP(...) return index matrices I and J so that Y = X(I) and X = Y(J).
 
X can be a numeric or a cell array.

Version 2.0 (nov 2007)

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
SHAKE

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
30 Oct 2007 Yidong Li

Unfortunately, it doesn't work for me. First thing confused me:
the line 68, all the "dim" should be "dimmode", is that right?
Second thing: Even I modified them, I still couldn't get the correct answer to "randswap(X,1)". The result seems like "randswap(X)".
Can you other guys check it as well?

21 Nov 2007 Jos (the author)

Thanks Yidong for pointing out the errors. I am very sorry and will update the file shortly. Jos

09 May 2009 Kevin Grey

Great! This is exactly what I wanted. For network analysis of Quadratic Assignment Procedure (QAP), this step is pre-required.

31 Mar 2010 grega

Thank you for this great function!

Would it be possible to update the function with an argument specifying distribution also?

I suppose randomization is done according to uniform distribution. It would be great if it works also with other, e.g. normal, binomial, poisson, inverse of normal etc.

Thank you!

27 Sep 2010 Andrey Sokolov

For QAP, I need to scramble rows and columns of the adjacency matrix in exactly the same way. It would be nice if there was an option to apply the same permutation to both dimensions for scrambling square matrices.

27 Sep 2010 Jos (10584)

@Andrey, you could accomplish this using the 2nd output of randswap:

a = reshape(1:25,5,5)
[ra,i] = randswap(a,1) % swap rows
ra = ra.' ; % transpose (columns become rows)
ra = ra(i) ; % swap rows again
ra = ra.' ; transpose again

27 Sep 2010 Jos (10584)

@Andrey ... or use randperm directly, of course

ri = randperm(size(a,1))
ra = a(ri,ri)

11 Sep 2011 ALiveris

thanks a lot, worked good with my cell array

Please login to add a comment or rating.
Updates
26 Nov 2007

fixed serious bug with dimmode

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 08:43:33
randomize Jos (10584) 23 Oct 2008 06:28:57
swap Jos (10584) 23 Oct 2008 06:28:57
rand Jos (10584) 23 Oct 2008 06:28:57
random Jos (10584) 23 Oct 2008 06:28:57
matrix Jos (10584) 23 Oct 2008 06:28:57
interchange Jos (10584) 23 Oct 2008 06:28:57
select Jos (10584) 23 Oct 2008 06:28:57
shuffle Jos (10584) 23 Oct 2008 06:28:57
shake Jos (10584) 23 Oct 2008 06:28:57

Contact us at files@mathworks.com