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:
random sample of n rows from matrix

Subject: random sample of n rows from matrix

From: Ulrik Nash

Date: 14 Dec, 2009 13:41:07

Message: 1 of 6

I have the following code:

m = [1 23, 2 34, 3 45, 4 43]

Now I want to draw a random sample of n rows from m.

I have tried the following:

sample = m(randperm(n))

but I only get values from the first column.

What is the correct proceedure?

Subject: random sample of n rows from matrix

From: Loren Shure

Date: 14 Dec, 2009 13:56:23

Message: 2 of 6

In article <hg5f9j$nrl$1@fred.mathworks.com>, uwn@sam.sdu.dk says...
> I have the following code:
>
> m = [1 23, 2 34, 3 45, 4 43]
>
> Now I want to draw a random sample of n rows from m.
>
> I have tried the following:
>
> sample = m(randperm(n))
>
> but I only get values from the first column.
>
> What is the correct proceedure?
>

sample = m(randperm(n),:)

--
Loren
http://blogs.mathworks.com/loren

Subject: random sample of n rows from matrix

From: Ulrik Nash

Date: 14 Dec, 2009 14:08:02

Message: 3 of 6

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.25900e10477e0356989a8d@news.mathworks.com>...
> In article <hg5f9j$nrl$1@fred.mathworks.com>, uwn@sam.sdu.dk says...
> > I have the following code:
> >
> > m = [1 23, 2 34, 3 45, 4 43]
> >
> > Now I want to draw a random sample of n rows from m.
> >
> > I have tried the following:
> >
> > sample = m(randperm(n))
> >
> > but I only get values from the first column.
> >
> > What is the correct proceedure?
> >
>
> sample = m(randperm(n),:)
>
> --
> Loren
> http://blogs.mathworks.com/loren

Thanks Loren!

Subject: random sample of n rows from matrix

From: Hsing

Date: 28 Mar, 2011 16:47:03

Message: 4 of 6

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hg5f9j$nrl$1@fred.mathworks.com>...
> I have the following code:
>
> m = [1 23, 2 34, 3 45, 4 43]
>
> Now I want to draw a random sample of n rows from m.
>
> I have tried the following:
>
> sample = m(randperm(n))
>
> but I only get values from the first column.
>
> What is the correct proceedure?

Suppose you have a matrix m of N rows and you would like to take a random sample of n rows from m, try

sample = m(randsample(1:N,n),:)
randsample(1:N,n) gives you a sequence of n random integers from 1 to N.

Hsing
29.Mar.2011

Subject: random sample of n rows from matrix

From: Chris

Date: 23 Apr, 2011 05:07:08

Message: 5 of 6

I would do the following:
y = randsample(m,n);
Here m is the population, which is the string of numbers you want to sample
and n is the length of the vector you want to sample
so lets say from vector m you want to randomly sample 10 numbers.Do the following
y = randsample(m,10)

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hg5f9j$nrl$1@fred.mathworks.com>...
> I have the following code:
>
> m = [1 23, 2 34, 3 45, 4 43]
>
> Now I want to draw a random sample of n rows from m.
>
> I have tried the following:
>
> sample = m(randperm(n))
>
> but I only get values from the first column.
>
> What is the correct proceedure?

Subject: random sample of n rows from matrix

From: koji takahashi

Date: 16 Jan, 2012 19:40:08

Message: 6 of 6

this is spot-on! Thanks Hsing!
sample = m(randsample(1:length(m),n),:); % n = number of random selections


"Hsing " <umchang5@cc.umanitoba.ca> wrote in message <imqe27$4nk$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hg5f9j$nrl$1@fred.mathworks.com>...
> > I have the following code:
> >
> > m = [1 23, 2 34, 3 45, 4 43]
> >
> > Now I want to draw a random sample of n rows from m.
> >
> > I have tried the following:
> >
> > sample = m(randperm(n))
> >
> > but I only get values from the first column.
> >
> > What is the correct proceedure?
>
> Suppose you have a matrix m of N rows and you would like to take a random sample of n rows from m, try
>
> sample = m(randsample(1:N,n),:)
> randsample(1:N,n) gives you a sequence of n random integers from 1 to N.
>
> Hsing
> 29.Mar.2011

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