Permutation without using perms, randperm, randsample
이전 댓글 표시
I need to write a matlab function to receive an integer (n) and then have a vector for 1:n in a random order. I need to do this without using perms, randperm, or randsample. I have this code:
function output=permutation(n)
array=1:n;
size_a=size(array);
size_a=size_a(2);
for i=1:size_a
d=randi(numel(array));
newArray(i)=d;
if find(newArray(i))==d
d=randi(numel(array));
newArray(i)=d;
end
end
output=newArray
end
I just can't figure out how to get it to not repeat values.
채택된 답변
추가 답변 (1개)
Walter Roberson
2016년 4월 27일
0 개 추천
You can use ismember to test to see if d is already in the array.
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!