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:
the output of 'combnk' is in random order ???

Subject: the output of 'combnk' is in random order ???

From: Xiaoyu

Date: 18 Jan, 2013 19:00:09

Message: 1 of 3

Hi, I just realize that 'combnk' produce output in some kind of random order. Typically, for combnk(1:N,2)', N=2,3,4,...10, the outut is given as:

combnk(1:4,2)'
ans =

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

while

combnk(1:6,2)'

ans =

  Columns 1 through 10

     1 1 1 1 1 2 2 2 2 3
     2 3 4 5 6 3 4 5 6 4

  Columns 11 through 15

     3 3 4 4 5
     5 6 5 6 6

The question is how can I get the outputs all in ascending order, like all in the form of
 1 1 1 1 1 2 2 2 2 3 ...
 2 3 4 5 6 3 4 5 6 4 ...

Apparently, 'sort' is not useful, and the randomness of 'combnk' does not mean the output might just be in a revsere order, for example, in combnk(1:25,13)'
the result in the first column is

     1 2 3 4 5 6 7 8 9 22 23 24 25

but what I would expect is (1,2,3,4,5...13) ,what code can I use to arrange the output of 'combnk' in such order?

Please help!

Subject: the output of 'combnk' is in random order ???

From: Roger Stafford

Date: 18 Jan, 2013 20:20:10

Message: 2 of 3

"Xiaoyu " <xc1u09@soton.ac.uk> wrote in message <kdc63p$n7a$1@newscl01ah.mathworks.com>...
> Hi, I just realize that 'combnk' produce output in some kind of random order. .....
> .........
> The question is how can I get the outputs all in ascending order, like all in the form of
> 1 1 1 1 1 2 2 2 2 3 ...
> 2 3 4 5 6 3 4 5 6 4 ...
- - - - - - - - -
  Keep the combinations in rows and use 'sortrows'.

 c = combnk(1:n,2);
 c = sortrows(c).';

Roger Stafford

Subject: the output of 'combnk' is in random order ???

From: Tom Lane

Date: 18 Jan, 2013 22:04:38

Message: 3 of 3

>> Hi, I just realize that 'combnk' produce output in some kind of random
>> order. .....
...
> Keep the combinations in rows and use 'sortrows'.

Or you might like nchoosek better. The results look less haphazard.

-- Tom

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