How to delete k in random n set number?
Show older comments
Answers (1)
Steven Lord
on 29 Sep 2020
0 votes
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 Comments
Muhammad Sam'an
on 29 Sep 2020
Steven Lord
on 29 Sep 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)
Muhammad Sam'an
on 29 Sep 2020
Categories
Find more on Share and Distribute Software 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!