Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
make random numbers from a vector

Subject: make random numbers from a vector

From: samira

Date: 10 Jun, 2011 19:16:02

Message: 1 of 5

Hi I would like to define a vector of my desired numbers and then ask matlab to generate random numbers only form the number I have in the vector.

for example this is my vector [ 0.2, 0.6, 1.2, 0.7]

so my results after generating random numbers by matlab should only consist of these 4 values.

Thanks for your help

Subject: make random numbers from a vector

From: Roger Stafford

Date: 10 Jun, 2011 19:25:23

Message: 2 of 5

"samira" wrote in message <istqhi$fjr$1@newscl01ah.mathworks.com>...
> Hi I would like to define a vector of my desired numbers and then ask matlab to generate random numbers only form the number I have in the vector.
>
> for example this is my vector [ 0.2, 0.6, 1.2, 0.7]
>
> so my results after generating random numbers by matlab should only consist of these 4 values.
>
> Thanks for your help
- - - - - - - -
 v = [ 0.2, 0.6, 1.2, 0.7];
 n = 100;
 x = v(ceil(length(v)*rand(1,n)));

Roger Stafford

Subject: make random numbers from a vector

From: Steven_Lord

Date: 10 Jun, 2011 20:37:15

Message: 3 of 5



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:istr33$hfk$1@newscl01ah.mathworks.com...
> "samira" wrote in message <istqhi$fjr$1@newscl01ah.mathworks.com>...
>> Hi I would like to define a vector of my desired numbers and then ask
>> matlab to generate random numbers only form the number I have in the
>> vector.
>>
>> for example this is my vector [ 0.2, 0.6, 1.2, 0.7]
>>
>> so my results after generating random numbers by matlab should only
>> consist of these 4 values.
>>
>> Thanks for your help
> - - - - - - - -
> v = [ 0.2, 0.6, 1.2, 0.7];
> n = 100;
> x = v(ceil(length(v)*rand(1,n)));

Or if you're using a newer version, you can use RANDI.

v = [0.2 0.6 1.2 0.7];
n = 100;
x = v(randi([1 numel(v)], 1, n));

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: make random numbers from a vector

From: Sadik

Date: 11 Jun, 2011 18:20:05

Message: 4 of 5

Or,

v = [0.2 0.6 1.2 0.7];
n = 100;
x = randsample(v,n,true);

Best.

Subject: make random numbers from a vector

From: samira

Date: 11 Jun, 2011 19:28:05

Message: 5 of 5

"Sadik " <sadik.hava@gmail.com> wrote in message <it0bkl$rj$1@newscl01ah.mathworks.com>...
> Or,
>
> v = [0.2 0.6 1.2 0.7];
> n = 100;
> x = randsample(v,n,true);
>
> Best



THANKS EVERYBODY
I'll try all points.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us