How do I make a script that outputs all numbers containing a certain set of digits?
Show older comments
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 = 0
while 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
end
end
but it is impressively inelegant and simply takes too long for larger amounts of numbers.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!