How do I make a script that outputs all numbers containing a certain set of digits?
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!