Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: generate specific random value except some numbers
Replies: 6   Last Post: Jul 15, 2013 5:26 AM

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,273 Registered: 2/7/05
Re: generate specific random value except some numbers
Posted: Jul 14, 2013 11:32 PM

On 7/14/2013 2:43 AM, Antonio Valentine wrote:

>>
>> -----------------------------
>> A = [1:9 11 12 13:21 23:26 28:40];
>> N = length(A);
>> num_event = 10;
>> arrayfun(@(i) A(randi(N,1)),1:num_event)
>> --------------------------------
>>
>> 9 37 6 34 21 40 3 18 4 39

>
> Your suggestion is quite straightforward.

But note: The above can end up picking the same element from
the set more than one time. Still by random, but you can
end up with the same element selected more than once.

If you want to randomly select say m elements from a set
without the same element selected again, you can try this:

------------------------------
A = [1,2,3,5,6,7,11,12,20,21]; %the set
m = 5; %number of elements to pick by random
B = zeros(m,1); %place to store what was selected
for i = 1:m
k = randi(length(A),1);
B(i) = A(k);
A(k) = []; %remove what was selected so it won't be selected again
end
B
--------------------------

>May I ask for an option of
>
> A = [1:40];
> B = [10 13 22 27];
>
> Then I can separate into two vectors where vector B the numbers I want to skip it.
>
> Is that possible?
>

sorry, do not understand what is asked here.

--Nasser

Date Subject Author
7/14/13 Antonio Valentine
7/14/13 Nasser Abbasi
7/14/13 Antonio Valentine
7/14/13 Bruno Luong
7/14/13 Antonio Valentine
7/14/13 Nasser Abbasi
7/15/13 Roger Stafford