Just a small problem
Afficher commentaires plus anciens
Hi! Can you help me please with this problem: How can I Write a function that produces random integers From 1000 to 9999, and including the numbers of different 4-digit (with no repetition)? In MatLab...
2 commentaires
Andrew Newell
le 30 Déc 2011
For a start, see randint: http://www.mathworks.com/help/toolbox/comm/ref/randint.html.
Izmir
le 30 Déc 2011
Réponse acceptée
Plus de réponses (3)
Jan
le 31 Déc 2011
Finite and no large table:
a = 0:9;
i1 = ceil(rand * 9);
a(i1 + 1) = [];
p = randperm(9);
result = i1 * 1000 + a(p(1:3)) * [100; 10; 1]
2 commentaires
Fangjun Jiang
le 31 Déc 2011
B+
Daniel Shub
le 31 Déc 2011
This is how I would have done it.
Jan
le 30 Déc 2011
It really looks like a homework and your teacher/professor will not recommend this forum to other students any longer after your homework has been solved here. Be sure to mention the assistence you got, otherwise it would be cheating.
I'd prefer a solution, which is guaranteed to stop in finite time. But I have only this idea:
a = 0:9;
while true
b = a(randperm(10, 4)); % Matlab 2011b required
if b(1) ~= 0
break;
end
end
result = b * [1000; 100; 10; 1]
For earlier Matlab versions:
b = a(randperm(10));
...
result = b(1:4) * [1000; 100; 10; 1]
1 commentaire
Fangjun Jiang
le 31 Déc 2011
B+
Jan
le 30 Déc 2011
An algorithm in finite time, but not really efficient:
K = perms(0:9);
K(K(:,1) == 0, :) = [];
index = ceil(rand * size(K, 1));
result = K(index, 1:4) * [1000; 100; 10; 1]
It is more efficient to create a [Nx4] table only instead of the [Nx10] table of perms:
K = VChooseKO(0:9, 4);
1 commentaire
Fangjun Jiang
le 31 Déc 2011
B+
Catégories
En savoir plus sur Get Started with MATLAB 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!