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 sampling of rows

Subject: random sampling of rows

From: ravi

Date: 23 Jun, 2010 00:00:23

Message: 1 of 6

Hi guys,


I have the following matrix:

X = [ 1 2 3; 4 5 6; 7 8 9]

In matlab it looks like this:
>> X = [ 1 2 3; 4 5 6; 7 8 9]

X =

     1 2 3
     4 5 6
     7 8 9

However, if I want to get X to output with randomly sampled rows for example,:

X =

     4 5 6
     1 2 3
     7 8 9

OR

X =

     7 8 9
     4 5 6
     1 2 3

and so forth for all different possible combinations , what can i do?


Thanks
Ravi
     

Subject: random sampling of rows

From: Wayne King

Date: 23 Jun, 2010 00:20:16

Message: 2 of 6

"ravi " <ravi_071@hotmail.com> wrote in message <hvriqn$h5g$1@fred.mathworks.com>...
> Hi guys,
>
>
> I have the following matrix:
>
> X = [ 1 2 3; 4 5 6; 7 8 9]
>
> In matlab it looks like this:
> >> X = [ 1 2 3; 4 5 6; 7 8 9]
>
> X =
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> However, if I want to get X to output with randomly sampled rows for example,:
>
> X =
>
> 4 5 6
> 1 2 3
> 7 8 9
>
> OR
>
> X =
>
> 7 8 9
> 4 5 6
> 1 2 3
>
> and so forth for all different possible combinations , what can i do?
>
>
> Thanks
> Ravi
>

Hi Ravi, Do you just want randomly-selected permuations? or are you really looking for all possibilities? I wasn't sure from your post. If you want to get random permutations:

X = [ 1 2 3; 4 5 6; 7 8 9];
Indices = randperm(size(X,1));
X(Indices,:)

Wayne

Subject: random sampling of rows

From: Matt Fig

Date: 23 Jun, 2010 00:21:07

Message: 3 of 6

x(randperm(size(x,1)),:)

Subject: random sampling of rows

From: ravi

Date: 23 Jun, 2010 00:29:23

Message: 4 of 6

hi wayne,

i'm looking for all possibilities.

thanks for your help
ravi

Subject: random sampling of rows

From: Matt Fig

Date: 23 Jun, 2010 01:28:21

Message: 5 of 6

% Sample data
x = reshape(randperm(8),4,2);

% One of many approaches to the problem: Store result in a cell array.
D = size(x,1);
D = mat2cell(perms(1:D),ones(prod(1:D),1),D);
D = cellfun(@(t) x(t,:),D,'Un',0)

Subject: random sampling of rows

From: Matt Fig

Date: 23 Jun, 2010 03:37:21

Message: 6 of 6

Or, if you prefer explicit FOR loops:

% Another, probably faster method.
idx = perms(1:size(x,1));
C = cell(size(idx,1),1);

for ii = 1:size(idx,1)
   C{ii} = x(idx(ii,:),:);
end

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