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:
Randomly permute a given set of numbers

Subject: Randomly permute a given set of numbers

From: Venkatesh

Date: 12 Sep, 2010 06:00:05

Message: 1 of 12

I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?

The matrix should look somewhat like this (randomly generated):

     0 0 4 1 2 3
     3 1 2 0 0 0
     0 2 3 0 4 1
     0 0 1 0 0 2
     3 0 0 1 0 2

Subject: Randomly permute a given set of numbers

From: Ross W

Date: 12 Sep, 2010 06:09:04

Message: 2 of 12

"Venkatesh " <svthegreat@gmail.com> wrote in message <i6hq95$fjn$1@fred.mathworks.com>...
> I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?
>
> The matrix should look somewhat like this (randomly generated):
>
> 0 0 4 1 2 3
> 3 1 2 0 0 0
> 0 2 3 0 4 1
> 0 0 1 0 0 2
> 3 0 0 1 0 2

doc randperm

Subject: Randomly permute a given set of numbers

From: Venkatesh

Date: 12 Sep, 2010 06:24:05

Message: 3 of 12

randperm(n) generates integers from 1 to n. So it can be used to generate the integers, but what about filling the remaining positions by zeros?


"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i6hqq0$ilg$1@fred.mathworks.com>...
> "Venkatesh " <svthegreat@gmail.com> wrote in message <i6hq95$fjn$1@fred.mathworks.com>...
> > I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?
> >
> > The matrix should look somewhat like this (randomly generated):
> >
> > 0 0 4 1 2 3
> > 3 1 2 0 0 0
> > 0 2 3 0 4 1
> > 0 0 1 0 0 2
> > 3 0 0 1 0 2
>
> doc randperm

Subject: Randomly permute a given set of numbers

From: Matt Fig

Date: 12 Sep, 2010 06:38:03

Message: 4 of 12

"Venkatesh " <svthegreat@gmail.com> wrote in message <i6hq95$fjn$1@fred.mathworks.com>...
> I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?
>
> The matrix should look somewhat like this (randomly generated):
>
> 0 0 4 1 2 3
> 3 1 2 0 0 0
> 0 2 3 0 4 1
> 0 0 1 0 0 2
> 3 0 0 1 0 2

Why do you say "between 2 and m" when you have a 1 and 0 in each row? What, _exactly_ are the rules?

Subject: Randomly permute a given set of numbers

From: Venkatesh

Date: 12 Sep, 2010 07:05:06

Message: 5 of 12

Sorry, my mistake. The _no. of integers_ generated must be in the range 2 to m.

The condition is that each row must contain _at least_ two non-zero integer values.
Therefore, each row should have 'a' zeros ('a' ranges from 0 to m-2) and 'b' integers ('b' ranges from 2 to m). The values of the integers are 1,2,...,m - a.

For example, take the first row(m=6). It has two zeros (a=2) and b = m - 2 = 4. Therefore, the values of integers = 1,2,3,4.

Also, is there any command to randomly arrange a given set of random numbers, say {13,4,5,6,2} ?


"Matt Fig" <spamanon@yahoo.com> wrote in message <i6hsgb$3oh$1@fred.mathworks.com>...
> "Venkatesh " <svthegreat@gmail.com> wrote in message <i6hq95$fjn$1@fred.mathworks.com>...
> > I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?
> >
> > The matrix should look somewhat like this (randomly generated):
> >
> > 0 0 4 1 2 3
> > 3 1 2 0 0 0
> > 0 2 3 0 4 1
> > 0 0 1 0 0 2
> > 3 0 0 1 0 2
>
> Why do you say "between 2 and m" when you have a 1 and 0 in each row? What, _exactly_ are the rules?

Subject: Randomly permute a given set of numbers

From: Ross W

Date: 12 Sep, 2010 07:21:06

Message: 6 of 12

"Venkatesh " <svthegreat@gmail.com> wrote in message <i6hu32$cb$1@fred.mathworks.com>...
> Sorry, my mistake. The _no. of integers_ generated must be in the range 2 to m.
>
> The condition is that each row must contain _at least_ two non-zero integer values.
> Therefore, each row should have 'a' zeros ('a' ranges from 0 to m-2) and 'b' integers ('b' ranges from 2 to m). The values of the integers are 1,2,...,m - a.
>
> For example, take the first row(m=6). It has two zeros (a=2) and b = m - 2 = 4. Therefore, the values of integers = 1,2,3,4.
>
> Also, is there any command to randomly arrange a given set of random numbers, say {13,4,5,6,2} ?

yes it's called randperm

>
>
> "Matt Fig" <spamanon@yahoo.com> wrote in message <i6hsgb$3oh$1@fred.mathworks.com>...
> > "Venkatesh " <svthegreat@gmail.com> wrote in message <i6hq95$fjn$1@fred.mathworks.com>...
> > > I have a question wherein I am required to generate integers between 2 and m randomly, without repetition (in an n x m matrix). Therefore, total number of integers generated in a row must be m, with at least two non-zero values. Any way to do this?
> > >
> > > The matrix should look somewhat like this (randomly generated):
> > >
> > > 0 0 4 1 2 3
> > > 3 1 2 0 0 0
> > > 0 2 3 0 4 1
> > > 0 0 1 0 0 2
> > > 3 0 0 1 0 2
> >
> > Why do you say "between 2 and m" when you have a 1 and 0 in each row? What, _exactly_ are the rules?

Subject: Randomly permute a given set of numbers

From: Bruno Luong

Date: 12 Sep, 2010 08:41:07

Message: 7 of 12

n=100; m=5;

[trash A] = sort(rand(n,m),2);
p = arrayfun(@(k) nchoosek(m,k), 2:m);
c = [0 cumsum(p)];
r = rand(size(A,1),1);
[trash i] = histc(r*c(end),c);
nz = i+1;
A = max(bsxfun(@minus,A,nz),0)

% Bruno

Subject: Randomly permute a given set of numbers

From: Bruno Luong

Date: 12 Sep, 2010 09:00:07

Message: 8 of 12

To generate random array having an equiprobability for each individual combination, this code should be used:

n=100; m=6;

[trash A] = sort(rand(n,m),2);
p = arrayfun(@(k) nchoosek(m,k)*factorial(m-k), 2:m); % Change here
c = [0 cumsum(p)];
r = rand(size(A,1),1);
[trash i] = histc(r*c(end),c);
nz = i+1;
A = max(bsxfun(@minus,A,nz),0)

% Bruno

Subject: Randomly permute a given set of numbers

From: Roger Stafford

Date: 12 Sep, 2010 18:22:03

Message: 9 of 12

  Venkatesh didn't make it clear what relative frequencies are desired for the different nonzero row counts. If each count is assumed to have an equal probability, one could simply do this:

 A = zeros(n,m);
 for k = 1:n
  p = randperm(m);
  q = 1:floor((m-1)*rand+2);
  A(k,p(q)) = q;
 end

Roger Stafford

Subject: Randomly permute a given set of numbers

From: Bruno Luong

Date: 12 Sep, 2010 19:07:04

Message: 10 of 12

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6j5ob$i4i$1@fred.mathworks.com>...
> Venkatesh didn't make it clear what relative frequencies are desired for the different nonzero row counts. If each count is assumed to have an equal probability, one could simply do this:
>
> A = zeros(n,m);
> for k = 1:n
> p = randperm(m);
> q = 1:floor((m-1)*rand+2);
> A(k,p(q)) = q;
> end

Roger, I believe OP wants there are minimum two zeros by row.

Bruno

Subject: Randomly permute a given set of numbers

From: Roger Stafford

Date: 12 Sep, 2010 19:39:03

Message: 11 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i6j8co$6h7$1@fred.mathworks.com>...
> Roger, I believe OP wants there are minimum two zeros by row.
>
> Bruno
- - - - - -
  Venkatesh said, "Sorry, my mistake. The _no. of integers_ generated must be in the range 2 to m. The condition is that each row must contain _at least_ two non-zero integer values. Therefore, each row should have 'a' zeros ('a' ranges from 0 to m-2) ...." I interpret this to mean that my quantity 'q' should range anywhere from 1:2 up to 1:m, which is what I have done here. In other words the number of zeros should have a maximum of m-2, not a minimum of 2. Admittedly in the given example each row did have at least two zeros but I don't believe that condition was stated and his quoted range of nonzeros from 2 to m contradicts that.

Roger Stafford

Subject: Randomly permute a given set of numbers

From: Bruno Luong

Date: 12 Sep, 2010 19:55:04

Message: 12 of 12

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6ja8n$4kk$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i6j8co$6h7$1@fred.mathworks.com>...
> > Roger, I believe OP wants there are minimum two zeros by row.
> >
> > Bruno
> - - - - - -
> Venkatesh said, "Sorry, my mistake. The _no. of integers_ generated must be in the range 2 to m. The condition is that each row must contain _at least_ two non-zero integer values. Therefore, each row should have 'a' zeros ('a' ranges from 0 to m-2) ...." I interpret this to mean that my quantity 'q' should range anywhere from 1:2 up to 1:m, which is what I have done here. In other words the number of zeros should have a maximum of m-2, not a minimum of 2. Admittedly in the given example each row did have at least two zeros but I don't believe that condition was stated and his quoted range of nonzeros from 2 to m contradicts that.
>
> Roger Stafford

That's right my bad, I missread "two non-zeros" and somehow translate it to "two zeros". Coffee time, LOL. In that case I must change my algorithm.

n=10000;
m=6;

[trash A] = sort(rand(n,m),2);
p = arrayfun(@(k) nchoosek(m,k)*factorial(m-k), 0:m-2); % Change here
c = [0 cumsum(p)];
r = rand(size(A,1),1);
[trash i] = histc(r*c(end),c);
nz = i-1;
A = max(bsxfun(@minus,A,nz),0)

Bruno

Tags for 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