Random shuffle vector such that all elements fall in new index positions

63 vues (au cours des 30 derniers jours)
Hello community. The title is quite self explanatory. Suppose I have the vector:
v = [1 2 3 4 5 6 7]
I want to shuffle the elements of v such that there is no index repetition with previous order. For example, this is not good
6 5 3 1 7 2 4
because '3' falls in the same place as before. This is good:
7 6 2 1 4 5 3
I am performing the shuffling with:
v(randperm(length(v)))
Of course this is just an example, I will be working with quite large arrays, so jumping into for or while loops to check weather new permutation is ok or not (and make new permutation if so) is time consuming. Any ideas or suggestions will be much appreciated. Cheers.

Réponse acceptée

Stephen23
Stephen23 le 29 Nov 2019
Modifié(e) : Stephen23 le 1 Mar 2021
What you describe is called a derangement.
You can download FEX submissions that perform derangements:
For example:
Note that Jan's submission uses a mex function, and should be quite efficient on your large arrays.
  1 commentaire
Javier Agustin Romero
Javier Agustin Romero le 29 Nov 2019
I didn't know this type of permutation was called a derangement, so thank you for that.
And Jan's function works amazingly well.
I will crown your answer as the accepted one, but I thank everyone who contributed to this thread.

Connectez-vous pour commenter.

Plus de réponses (2)

Stijn Haenen
Stijn Haenen le 29 Nov 2019
Try this:
v=[1:100]; %your vector
List=1:numel(v);
for i=1:numel(v)
newList=List;
newList(i)=[];
newpos=datasample(newList(newList>0),1);
List(newpos)=0;
newVector(newpos)=v(i);
end
  1 commentaire
Guillaume
Guillaume le 29 Nov 2019
An explanation of why it works, or even better some comments to the code would be useful.
In particular, I don't see how your code guarantees that when your reach index 100 the only index left to pick is not 100. For example, if datasample returned the sequence [2:99, 1] on the last step of the loop, the only index left to pick is 100. Your code would simply discard it and return a new vector with one less element

Connectez-vous pour commenter.


Guillaume
Guillaume le 29 Nov 2019
Modifié(e) : Guillaume le 29 Nov 2019
I would:
  • do a normal randperm that may map some elements back to their original location
  • any element that has been mapped to its original location, swap it with any other element, guaranteeing you that the two swapped elements are not at their original location
v = 1:2000; %demo vector. Can have duplicate values
neworder = randperm(numel(v)); %may result in some indices mapping to themselves
hasnotmoved = find(neworder == 1:numel(v), 1); %is there any index that has mapped to itself. If so pick the first one
while hasnotmoved %go over them one by one
available = [1:hasnotmoved-1, hasnotmoved+1:numel(v)]; %list of indices other than the one that did not move
swapwith = available(randi(numel(v)-1)); %choose one index at random other than it
neworder([swapwith, hasnotmoved]) = [hasnotmoved, swapwith]; %and swap the two
hasnotmoved = find(neworder == 1:numel(v), 1); %any more?
end
%now we're guaranteed that no index mapped to itself
assert(all(neworder ~= 1:numel(v)), 'Did not work!')
newv = v(neworder);
In the majority of cases there's very few elements that mapped back to each other, so the while loop will do very few iterations. Worst case scenario, randperm did not swap anything and the while loop swaps all elements one by one.
edit: initial implementation was slightly flawed
  2 commentaires
Stephen23
Stephen23 le 29 Nov 2019
Modifié(e) : Stephen23 le 29 Nov 2019
"...guaranteeing you that the two swapped elements are not at their original location"
How?
What stops an element from being moved back to its original location?
Guillaume
Guillaume le 29 Nov 2019
Modifié(e) : Guillaume le 29 Nov 2019
If you have the sequence
........P.......N......
where index of N is N, and index of P is Q (where Q may or may not be equal to P, doesn't matter), if you swap P and N:
........N.......P......
then index of N is now Q, with , and index of P is now N, with
edit: however there is a flaw in the way I've implemented the algorithm in that I may pick a Q that is an element that is going to be swapped later by the loop and is thus no longer the hasnotmoved element it was. A while loop instead of the for loop will fix that (and will have less than or the same number of iterations as the for loop).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by