Permutation without using perms, randperm, randsample
Show older comments
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 27 Apr 2016
0 votes
You can use ismember to test to see if d is already in the array.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!