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:
Randomly subsampling from matrix :(

Subject: Randomly subsampling from matrix :(

From: Woo Kim

Date: 6 May, 2010 13:09:04

Message: 1 of 4

Hi all I hope you're having nice days. I've spent way too much time on this problem... I need your help :(

I'll try to describe my problem clearly first, and then I'll just tell you exactly what I want to do. So I need to randomly subsample from a matrix. 'randsample' would be perfect, but I need to sample from a two-dimension array, and running randsample twice would mix up indices of the pairs. I thought about 'randi' to get indices first and then sample with them, but I noticed randi can return same value, which I have to avoid.

So flat out, I need to randomly sample 150 pairs of data from 250 pairs. (and I need 20 different sample sets) With studying 20 sample sets, I'm going to estimate the behavior of the original 250 pairs.

Sounds a lot like a homework right? :) yes, shamelessly I admit, this is a homework! But at least I'm doing it very much ahead of time and spent a lot of time by myself before asking here, so plz don't hate!

Subject: Randomly subsampling from matrix :(

From: Sean

Date: 6 May, 2010 13:30:23

Message: 2 of 4

"Woo Kim" <efreet84@hanmir.com> wrote in message <hruf1g$qua$1@fred.mathworks.com>...
> Hi all I hope you're having nice days. I've spent way too much time on this problem... I need your help :(
>
> I'll try to describe my problem clearly first, and then I'll just tell you exactly what I want to do. So I need to randomly subsample from a matrix. 'randsample' would be perfect, but I need to sample from a two-dimension array, and running randsample twice would mix up indices of the pairs. I thought about 'randi' to get indices first and then sample with them, but I noticed randi can return same value, which I have to avoid.
>
> So flat out, I need to randomly sample 150 pairs of data from 250 pairs. (and I need 20 different sample sets) With studying 20 sample sets, I'm going to estimate the behavior of the original 250 pairs.
>
> Sounds a lot like a homework right? :) yes, shamelessly I admit, this is a homework! But at least I'm doing it very much ahead of time and spent a lot of time by myself before asking here, so plz don't hate!

a hint:
>>shuffled_choices = randperm(250);
>>indices = shuffled_choices(1:150);
>>selected_data = matrix(indices);

You'll probably need to modify this a little.

Subject: Randomly subsampling from matrix :(

From: Steven Lord

Date: 6 May, 2010 14:34:14

Message: 3 of 4


"Woo Kim" <efreet84@hanmir.com> wrote in message
news:hruf1g$qua$1@fred.mathworks.com...
> Hi all I hope you're having nice days. I've spent way too much time on
> this problem... I need your help :(
>
> I'll try to describe my problem clearly first, and then I'll just tell you
> exactly what I want to do. So I need to randomly subsample from a matrix.
> 'randsample' would be perfect, but I need to sample from a two-dimension
> array, and running randsample twice would mix up indices of the pairs. I
> thought about 'randi' to get indices first and then sample with them, but
> I noticed randi can return same value, which I have to avoid.
>
> So flat out, I need to randomly sample 150 pairs of data from 250 pairs.
> (and I need 20 different sample sets) With studying 20 sample sets, I'm
> going to estimate the behavior of the original 250 pairs.
>
> Sounds a lot like a homework right? :) yes, shamelessly I admit, this is a
> homework! But at least I'm doing it very much ahead of time and spent a
> lot of time by myself before asking here, so plz don't hate!

I agree with Sean's suggestion that you should look at RANDPERM.

M = magic(4);
numElements = numel(M);
indicesOfAllPoints = randperm(numElements);
fiveRandomElements = M(indicesOfAllPoints(1:5))

This uses "linear indexing" -- you can search the documentation for that
term for more information. Alternately, if you want to randomly select a
subset of rows:

M = magic(10);
numRows = size(M, 1);
shuffledRows = randperm(numRows);
fiveRandomRows = M(shuffledRows(1:5), :)

But the procedure you describe sounds like you might be interested in this
section of the Statistics Toolbox documentation:

http://www.mathworks.com/access/helpdesk/help/toolbox/stats/f7462.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Randomly subsampling from matrix :(

From: Woo Kim

Date: 7 May, 2010 00:40:06

Message: 4 of 4

Thank you, Sean, and Steve Lord. That was exactly the solution I was looking for!

I did look up 'randperm' before (from another answer post by Steve Lord), but at that time I didn't think I could use it for my case... I guess this is the kind of difference between gurus and a noob.

I really enjoyed reading about Bootstrap sampling and its friends. Now I have to play around and see what kind of functions I can use for 'bootfun', but I'm pretty sure this is what I will probably end up using, also for many times in the future. Thank you so much again!

Tags for this Thread

No tags are associated with 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