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:
Coding

Subject: Coding

From: sivaprasad kunduru

Date: 22 Nov, 2010 13:08:03

Message: 1 of 4

Hello

I am having a binary vector a=[1,0,1,1,0,1,0,1,0,0,0,1,.....];
I want to sort this vector randomly,In other words I want to generate interleaver.

Can any one help me in this aspect.

Subject: Coding

From: Steven_Lord

Date: 22 Nov, 2010 14:35:36

Message: 2 of 4



"sivaprasad kunduru" <ku.sivaprasadreddy@gmail.com> wrote in message
news:icdpvj$hbc$1@fred.mathworks.com...
> Hello
>
> I am having a binary vector a=[1,0,1,1,0,1,0,1,0,0,0,1,.....];
> I want to sort this vector randomly,In other words I want to generate
> interleaver.
>
> Can any one help me in this aspect.

HELP RANDPERM.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Coding

From: Roger Stafford

Date: 22 Nov, 2010 23:41:04

Message: 3 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <icdv3o$6ph$1@fred.mathworks.com>...
> "sivaprasad kunduru" <ku.sivaprasadreddy@gmail.com> wrote in message
> news:icdpvj$hbc$1@fred.mathworks.com...
> > I am having a binary vector a=[1,0,1,1,0,1,0,1,0,0,0,1,.....];
> > I want to sort this vector randomly,In other words I want to generate
> > interleaver.
> > Can any one help me in this aspect.
> HELP RANDPERM.
- - - - - - - - - - - -
  Using 'randperm' is certainly the easiest solution to write. However, it poses an interesting question as to its algorithmic complexity. It requires a sort of n = length(a) random numbers and that means it is an order n*log(n) algorithm.

  The following code uses the same number of calls on 'rand' and requires many more lines of code, and yet is only of order n. Presumably there there must be a value of n beyond which this method would be faster, but I don't know at the moment what that number might be. It possesses the same statistics as would be given by 'randperm', namely that each possible combination with the same number of ones as in 'a' will be equally probable.

 n = length(a);
 p = n;
 q = sum(a);
 b = zeros(1,n);
 for k = 1:n
  if p*rand < q
   b(k) = 1;
   q = q-1;
  end
  p = p-1;
 end

Roger Stafford

Subject: Coding

From: Roger Stafford

Date: 23 Nov, 2010 00:30:07

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <icev2g$f2f$1@fred.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <icdv3o$6ph$1@fred.mathworks.com>...
> > "sivaprasad kunduru" <ku.sivaprasadreddy@gmail.com> wrote in message
> > news:icdpvj$hbc$1@fred.mathworks.com...
> > > I am having a binary vector a=[1,0,1,1,0,1,0,1,0,0,0,1,.....];
> > > I want to sort this vector randomly,In other words I want to generate
> > > interleaver.
> > > Can any one help me in this aspect.
> > HELP RANDPERM.
> The following code uses the same number of calls on 'rand' and requires many more lines of code, and yet is only of order n. Presumably there there must be a value of n beyond which this method would be faster, but I don't know at the moment what that number might be.
- - - - - - -
  Based on a few trial runs on my computer the break-even value of n would have to be very large indeed, so you should probably stick with Steven's 'randperm' method, Sivaprasad. Just regard the method I exhibited as something of theoretical interest.

Roger Stafford

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