Permutation without using perms, randperm, randsample
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 27 Apr. 2016
0 Stimmen
You can use ismember to test to see if d is already in the array.
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!