Taking a number out of a vector and not replacing it
3 views (last 30 days)
Show older comments
Hello World,
Suppose I have a code, where I am choosing a number out of a set, and once that number is chosen, i dont replace it. I want to then run the code mutiple times and not replace any numbers that have been chosen. So for example say i get : 10 10 10. I want to then run the code again without the 3 tens, then again without the 3 numbers that ive drawn. so then there will be 6 numbers less, i want to carry on doing this 20 times and there should be less numbers every time that i run it.
can somebody help me please, really struggling. Thanks Steven
v = [1,2,3,4,5,6,7,8,9,10,10,10,10];
x = [v,v,v,v,v,v];
i0 = (randi(length(x)));
j0 = (randi(length(x)));
k0 = (randi(length(x)));
while j0==i0
j0 = (randi(length(x)));
end
while k0==j0
while k0==i0
k0 = (randi(length(x)));
end
if k0==j0
k0 = (randi(length(x)));
end
end
Hand1 = [x(i0),x(j0)]
Hand2 = [x(k0)]
x(i0) = 0;
x(j0) = 0;
x(k0) = 0;
x
0 Comments
Answers (1)
Steven Lord
on 12 Mar 2020
Use randperm to generate a list of indices you can use to shuffle your list of data. Pull the first three elements from the output of randperm and use those as indices into your list of data. Next time, pull the next three elements from the output of randperm and use those as indices. Repeat this pattern until you're done or until you run out of elements.
If you know you only need a few elements from your long list of data, use the two input form of randperm to generate only as many indices as you need.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!