help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: random sort


From: Mike Miller
Subject: Re: random sort
Date: Wed, 3 Dec 2003 00:23:14 -0600 (CST)

On Tue, 2 Dec 2003, Heber Farnsworth wrote:

> I think your problem is that you are trying to sort something the same
> size as X when you really only want to sort a vector with as many
> elements as X has columns.
>
> If X is NxM then do
>
> [S I] = sort(rand(N,1));
> Y = X(:,I);
>
> and Y has the same columns as X but in shuffled order.

Yes, thanks, but I want to shuffle the elements with *every* column of X
independently.  I don't want to shuffle the rows of X.

Oh.  I just figured it out:

do_fortran_indexing = 1;
[N,M=size(X);
[S,I]=sort(rand(size(X)));
Y = X(ones(N,1)*[0:N:N*(M-1)]+I);

Then every column of Y is the corresponding column of X randomly sorted
(randomly sampled without replacement, if you prefer).  Thanks for keeping
me on track!


> You didn't mention whether you wanted to sample with replacement or not.
> This just re-orders the columns.  It's a different trick if you want to
> admit the possibility that some columns of X may not show up in Y while
> others are repeated.  That's what you would do if you were boostrapping
> data for instance.

Bootstrap resampling of rows can be done this way:

N=size(X,1);
X(:,ceil(N*rand(N,1)))

Best,

Mike

-- 
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]