Random shuffle vector such that all elements fall in new index positions
조회 수: 130 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
추가 답변 (2개)
Stijn Haenen
2019년 11월 29일
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
Guillaume
2019년 11월 29일
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
Guillaume
2019년 11월 29일
편집: Guillaume
2019년 11월 29일
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
Guillaume
2019년 11월 29일
편집: Guillaume
2019년 11월 29일
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).
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!