extracting rows in a matrix

Ivan Sutoris ivan.sutoris at gmail.com
Tue May 12 17:48:44 CDT 2009


On Wed, May 13, 2009 at 12:03 AM, Carlo Rossi <serosole at yahoo.it> wrote:
> Hello,
>  I have a matrix A 450x10  and I need t extract randomly 1/5 of 500 rows and
> create with these rows another matrix B.
> Is there any function to do it? Any helps?
>
> thanks,
>

Hi

Try:

nrows = size(A,1);
extract_row = rand(nrows,1) < 0.2;
B = A(extract_row, :);

extract_row will be logical vector where approx. 1/5 of elements are
1, rest is 0. This is then used to create B by logical indexing from
A.

Of course, if you need to extract exactly 1/5 of rows, or resample
rows with replacement, this won't work.

Regards
Ivan Sutoris



More information about the Help-octave mailing list