Thread Subject: -extract 120X9 matrix from 1000X9 matrix randomly

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: muk

Date: 9 Feb, 2010 16:04:04

Message: 1 of 9

Hello all,

Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.
How can i do that?

Sincerely,

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: Oleg Komarov

Date: 9 Feb, 2010 16:35:19

Message: 2 of 9

"muk " wrote in message
> Hello all,
>
> Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.
> How can i do that?
>
> Sincerely,

help rand

Example:
A = rand(1000,9);

rows = ceil(120*rand(120,1));
cols = ceil(9*rand(9,1));

randMat = A(rows,cols);

Oleg

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: Walter Roberson

Date: 9 Feb, 2010 16:35:38

Message: 3 of 9

muk wrote:

> Now, i have 1000X9 matrix which is quality parameters. And, i want to
> extract 120X9 matris randomly from 1000x9 matrix.
> How can i do that?

randperm(1000) and take the first 120 output values.

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: Cygnine

Date: 9 Feb, 2010 16:38:04

Message: 4 of 9

"muk " <hungocan@hotmail.com> wrote in message
> Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.

Do you mean:
a.) You want to randomly choose a 120x9 BLOCK from the bigger matrix, or
b.) You want to randomly choose 120*9 = 1080 elements from the big matrix?

For a:

>> big_matrix = ones([1000 9]); % or whatever data
>> starting_row = ceil(881*rand);
>> ending_row = starting_row + 119;
>> small_matrix = big_matrix(starting_row:ending_row,:);

For b:

>> big_matrix = ones([1000 9]);
>> small_matrix = reshape(big_matrix(ceil(1000*rand([1080 1]))), [120 9]);

You should of course worry about seeding the random number generator.

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: Cygnine

Date: 9 Feb, 2010 16:45:05

Message: 5 of 9

"Cygnine " <cygnine@remove.this.gmail.com> wrote in message
> Lots of stuff

Sorry, I realized I made a mistake, part b should be

>> small_matrix = reshape(big_matrix(ceil(9000*rand([1080 1]))), [120 9]);

And the other answers provided seem as good, if not simpler.

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: ade77

Date: 9 Feb, 2010 16:45:05

Message: 6 of 9

"muk " <hungocan@hotmail.com> wrote in message <hks11k$ti$1@fred.mathworks.com>...
> Hello all,
>
> Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.
> How can i do that?
>
> Sincerely,

Like Walter suggested, randperm seems to be your easiest way.

your_matrix = 1000 by 9
h = randperm(1000);
g = h(1:120);
rand_array = your_matrix(g,:);

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: us

Date: 9 Feb, 2010 17:23:04

Message: 7 of 9

"muk " <hungocan@hotmail.com> wrote in message <hks11k$ti$1@fred.mathworks.com>...
> Hello all,
>
> Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.
> How can i do that?
>
> Sincerely,

as others have said: it is not quite clear what you mean by a randomly selected submatrix...
assuming you want to carve out a randomly selected sub-block from your main mat,...

one of the solutions

% the data
     m=ceil(10*rand(5,4)); % <- main mat
     sr=2; % <- #rows of submat
     sc=3; % <- #cols of submat
% the engine
     [nr,nc]=size(m);
     [x,y]=meshgrid(1:nr:sc*nr,0:sr-1);
     seg=x+y; % <- SEG: indices into block at top/left
     off=nr*(nc-sc+1)-sr; % <- OFF: max movement of SEG
     roff=ceil(off*rand); % <- select random OFF
% the result
     disp(m);
     disp(m(seg+0)); % <- top/left submat
     disp(m(seg+off)); % <- last possible submat
     disp(roff); % <- random SEG
     disp(m(seg+roff)); % <- randomly selected submat
%{
% the mat
     2 2 6 6
     7 1 9 2
     6 5 1 6
     1 9 10 1
     1 7 2 8
% top/left
     2 2 6
     7 1 9
% last possible block given SR/SC
     9 10 1
     7 2 8
% random OFF
     7
% submat at random OFF
     5 1 6
     9 10 1
%}

us

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: us

Date: 9 Feb, 2010 17:32:04

Message: 8 of 9

"us "
...slight modification to the random OFF generator, which must be ]0:OFF[

% the data
     m=ceil(10*rand(5,4)); % <- main mat
     sr=2; % <- #rows of submat
     sc=3; % <- #cols of submat
% the engine
     [nr,nc]=size(m);
     [x,y]=meshgrid(1:nr:sc*nr,0:sr-1);
     seg=x+y; % <- SEG: indices into block at top/left
     off=nr*(nc-sc+1)-sr; % <- OFF: max movement of SEG
     roff=ceil((off+1)*rand)-1; % <- select random OFF
% the result
     disp(m);
     disp(m(seg+0)); % <- top/left submat
     disp(m(seg+off)); % <- last possible submat
     disp(roff); % <- random SEG
     disp(m(seg+roff)); % <- randomly selected submat
%{
% the mat
     2 2 6 6
     7 1 9 2
     6 5 1 6
     1 9 10 1
     1 7 2 8
% top/left: OFF = 0
     2 2 6
     7 1 9
% last possible block given SR/SC
     9 10 1
     7 2 8
% random OFF ]0:OFF[
     7
% submat at random OFF
     5 1 6
     9 10 1
%}

us

Subject: -extract 120X9 matrix from 1000X9 matrix randomly

From: muk

Date: 9 Feb, 2010 19:05:21

Message: 9 of 9

"ade77 " <ade100a@gmail.com> wrote in message <hks3eh$46u$1@fred.mathworks.com>...
> "muk " <hungocan@hotmail.com> wrote in message <hks11k$ti$1@fred.mathworks.com>...
> > Hello all,
> >
> > Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix.
> > How can i do that?
> >
> > Sincerely,
>
> Like Walter suggested, randperm seems to be your easiest way.
>
> your_matrix = 1000 by 9
> h = randperm(1000);
> g = h(1:120);
> rand_array = your_matrix(g,:);


Thank you Everyone!

That is the best choice and fast choice. There are many view to solve this problem.


> your_matrix = 1000 by 9
> h = randperm(1000);
> g = h(1:120);
> rand_array = your_matrix(g,:);

That is choice which is independent in data. May be, distributions of data may be important. But, there is not a problem. So, as i said ,this is the best choice, and clever choice.

Thank you all thank you "ade77" my friend.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
rand us 9 Feb, 2010 12:24:18
code us 9 Feb, 2010 12:24:18
meshgrid us 9 Feb, 2010 12:24:18
linear indexing us 9 Feb, 2010 12:24:18
matrix muk 9 Feb, 2010 11:04:09
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com