How to delete k in random n set number?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
0 comentarios
Respuestas (1)
Steven Lord
el 29 de Sept. de 2020
Use randperm to shuffle the numbers 1 through 20 and simply select the next element not yet chosen each time you need a random k.
3 comentarios
Steven Lord
el 29 de Sept. de 2020
randi generates numbers with replacement. randperm generates numbers without replacement.
rng default
x1 = randi(10, 1, 10)
x2 = randperm(10)
x1 contains the number 10 four times and doesn't contain 4, 5, or 8 at all.
x2 contains each number from 1 to 10 exactly once. You can iterate over the elements of that vector like so:
x2 = randperm(10);
for k = x2
fprintf("Processing element %d.\n", k)
end
% or
M = zeros(10);
for ind = 1:10
M(ind, x2(ind)) = ind;
end
disp(M)
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!