Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,660
Registered: 2/7/05
Re: generate specific random value except some numbers
Posted: Jul 14, 2013 11:32 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.