Say I want to make a vector containing all numbers with the digits [2,4,5,7,9], so 25497, 95742, etc. How can I make a script that puts all numbers in this vector?
I've tried the following:
%
vec = zeros (1,factorial(5));i = 0while i < factorial(5) num = 0; ii = 0; iii = [2,4,5,7,9]; while ii < 5; % This while loop creates a number
randnum = randi(5); eval = iii(randnum); if eval > 0 num = num + eval*10^(ii); iii(randnum) = 0; ii = ii + 1; end end if not(ismember(num,vec(:))) %This if-statement checks if the number had already been created earlier
i = i + 1; vec(i) = num; %If not, it is added to the vector
endend
but it is impressively inelegant and simply takes too long for larger amounts of numbers.
Best Answer