Code covered by the BSD License  

Highlights from
RANDSWAP

4.57143

4.6 | 7 ratings Rate this file 23 Downloads (last 30 days) File Size: 2.32 KB File ID: #12621

RANDSWAP

by

 

12 Oct 2006 (Updated )

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

Shake inspired this file.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
25 Mar 2014 Pernille

Thanks

05 Dec 2013 Fiqqih Faizah

it's great
thank you

17 May 2012 Florian  
11 Sep 2011 ALiveris

thanks a lot, worked good with my cell array

27 Sep 2010 Jos (10584)

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

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

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 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.

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!

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.

21 Nov 2007 Jos (the author)

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

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?

Contact us