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:
looking at all possible combinations

Subject: looking at all possible combinations

From: Ozge Taskan

Date: 17 May, 2010 14:28:20

Message: 1 of 12

Hi everyone,

I have a matrix like

S_new =

     2 6 11 0
     0 0 2 0
     0 1 2 0
     0 1 2 0
     1 3 6 0

and by taking one number from each row I would like to find each possible combinations of number for example:

2,0,0,0,1
2,0,0,0,3
2,0,0,0,6
2,0,0,0,0
6,0,0,0,1
6,0,0,0,3
...
.
.
.
11,0,2,1,6

one number is taken from first row, one number from second row, one number from third row .... like this but every time one number from one row( rows should be in orders from first,second, thurd, fourth, fifth row) and like this I should look at the every combinations, can it be possible?

Thank you in advance.

Kind regards,

Ozge

Subject: looking at all possible combinations

From: Matt Fig

Date: 17 May, 2010 14:57:04

Message: 2 of 12

One method:


% Data
A =[2 6 11 0;0 0 2 0;0 1 2 0;0 1 2 0;1 3 6 0]

% Engine
[m,n] = size(A);
G = A(npermutek(0:(n-1),m) * m + repmat(1:m,n^m,1));

Where npermutek is here:

http://www.mathworks.com/matlabcentral/fileexchange/11462-npermutek

Subject: looking at all possible combinations

From: Ozge Taskan

Date: 17 May, 2010 15:27:04

Message: 3 of 12

thank you very much, this is exactly what I want. Can I ask one more question? Is it possible to write the the rank of the number that is chosen in another matrix, I mean this time one matrix will write the rank of the numbers in the row? Is it possible?

Thank you very much in advance.

Ozge

Subject: looking at all possible combinations

From: Matt Fig

Date: 17 May, 2010 15:59:06

Message: 4 of 12

I do not know what you mean by "rank of a number." I usually think of the rank as the number of independent rows (or columns) in a matrix.

http://mathworld.wolfram.com/MatrixRank.html

Subject: looking at all possible combinations

From: Jos (10584)

Date: 17 May, 2010 16:07:05

Message: 5 of 12

"Ozge Taskan" <lordgy@yahoo.com> wrote in message <hsrjq4$9t0$1@fred.mathworks.com>...
> Hi everyone,
>
> I have a matrix like
>
> S_new =
>
> 2 6 11 0
> 0 0 2 0
> 0 1 2 0
> 0 1 2 0
> 1 3 6 0
>
> and by taking one number from each row I would like to find each possible combinations of number for example:
>
> 2,0,0,0,1
> 2,0,0,0,3
> 2,0,0,0,6
> 2,0,0,0,0
> 6,0,0,0,1
> 6,0,0,0,3
> ...
> .
> .
> .
> 11,0,2,1,6
>
> one number is taken from first row, one number from second row, one number from third row .... like this but every time one number from one row( rows should be in orders from first,second, thurd, fourth, fifth row) and like this I should look at the every combinations, can it be possible?
>
> Thank you in advance.
>
> Kind regards,
>
> Ozge

% a smaller example:
  A = [1 2 3 ; 11 12 13]
% engine
  RowRank = combn(1:size(A,2),size(A,1))
  B = A(sub2ind(size(A),repmat(1:size(A,1),size(RowRank,1),1),RowRank))

COMBN can be download on the FEX:
http://www.mathworks.com/matlabcentral/fileexchange/7147-combn

hth
Jos

Subject: looking at all possible combinations

From: Matt Fig

Date: 17 May, 2010 16:46:05

Message: 6 of 12

I see. If Jos is correct, then you can also do this with a slight modification of the solution I gave earlier.


[m,n] = size(A);
Rr = npermutek(1:n,m); % The "Row-Rank" matrix.
G = A((Rr-1)*m+repmat(1:m,n^m,1));

IIRC, COMBN and NPERMUTEK do the same thing using different methods.

Subject: looking at all possible combinations

From: Ozge Taskan

Date: 18 May, 2010 05:04:05

Message: 7 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <hsrrsd$o20$1@fred.mathworks.com>...
> I see. If Jos is correct, then you can also do this with a slight modification of the solution I gave earlier.
>
>
> [m,n] = size(A);
> Rr = npermutek(1:n,m); % The "Row-Rank" matrix.
> G = A((Rr-1)*m+repmat(1:m,n^m,1));
>
> IIRC, COMBN and NPERMUTEK do the same thing using different methods.


thank you very much.
ozge

Subject: looking at all possible combinations

From: Jan Simon

Date: 18 May, 2010 22:27:05

Message: 8 of 12

Dear Ozge!

> > IIRC, COMBN and NPERMUTEK do the same thing using different methods.

And if speed matters, the C-mex VChooseKRO might be helpful also:
  http://www.mathworks.com/matlabcentral/fileexchange/26242

Jan

Subject: looking at all possible combinations

From: Ozge Taskan

Date: 10 Jul, 2010 03:25:04

Message: 9 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <hsrrsd$o20$1@fred.mathworks.com>...
> I see. If Jos is correct, then you can also do this with a slight modification of the solution I gave earlier.
>
>
> [m,n] = size(A);
> Rr = npermutek(1:n,m); % The "Row-Rank" matrix.
> G = A((Rr-1)*m+repmat(1:m,n^m,1));
>
> IIRC, COMBN and NPERMUTEK do the same thing using different methods.

Hi,
I would like to ask something related to same thing.
If we have 9 numbers and we would like to assign one of the numbers below how can we look at each possibilities for example
1st number can be 1 or 4
2nd number can be 1 or 4
3rd number can be 4
4th number can be 1 or 2 or 3
5th number can be 1 or 2 or 3
6th number can be 1 or 2 or 3
7th number can be 3
8th number can be 2 or 3
9th number can be 1 or 2 or 3
how can we construct 2*2*1*3*3*3*1*2*3(648) numbers?

thank you very much in advance.

Subject: looking at all possible combinations

From: Ozge Taskan

Date: 10 Jul, 2010 04:51:04

Message: 10 of 12

"Ozge Taskan" <lordgy@yahoo.com> wrote in message <i18p6g$kri$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hsrrsd$o20$1@fred.mathworks.com>...
> > I see. If Jos is correct, then you can also do this with a slight modification of the solution I gave earlier.
> >
> >
> > [m,n] = size(A);
> > Rr = npermutek(1:n,m); % The "Row-Rank" matrix.
> > G = A((Rr-1)*m+repmat(1:m,n^m,1));
> >
> > IIRC, COMBN and NPERMUTEK do the same thing using different methods.
>
> Hi,
> I would like to ask something related to same thing.
> If we have 9 numbers and we would like to assign one of the numbers below how can we look at each possibilities for example
> 1st number can be 1 or 4
> 2nd number can be 1 or 4
> 3rd number can be 4
> 4th number can be 1 or 2 or 3
> 5th number can be 1 or 2 or 3
> 6th number can be 1 or 2 or 3
> 7th number can be 3
> 8th number can be 2 or 3
> 9th number can be 1 or 2 or 3
> how can we construct 2*2*1*3*3*3*1*2*3(648) combinations?
>
> thank you very much in advance.

Subject: looking at all possible combinations

From: Roger Stafford

Date: 10 Jul, 2010 06:22:05

Message: 11 of 12

"Ozge Taskan" <lordgy@yahoo.com> wrote in message <i18p6g$kri$1@fred.mathworks.com>...
> Hi,
> I would like to ask something related to same thing.
> If we have 9 numbers and we would like to assign one of the numbers below how can we look at each possibilities for example
> 1st number can be 1 or 4
> 2nd number can be 1 or 4
> 3rd number can be 4
> 4th number can be 1 or 2 or 3
> 5th number can be 1 or 2 or 3
> 6th number can be 1 or 2 or 3
> 7th number can be 3
> 8th number can be 2 or 3
> 9th number can be 1 or 2 or 3
> how can we construct 2*2*1*3*3*3*1*2*3(648) numbers?
>
> thank you very much in advance.
- - - - - - - - - -
  You can accomplish that using numbers with mixed bases. That is, start with the number 0 and count by ones up to 648-1. For each one of these numbers, compute the nine digits that would represent that number with the nine different bases 2, 2, 1, 3, 3, 3, 1, 2, 3. If 1 is added to each of these digits, they can be used as indices into a cell array containing all the number sets possible for each digit: the first digit can be 1 or 4, the second one also 1 or 4, the third one only 4, the fourth 1, 2, or 3, etc.

  For example, the number N = 556 can be represented in the mixed bases above by: 1 1 0 1 0 2 0 1 1 since

 556 = ((((((((1)*2+1)*1+0)*3+1)*3+0)*3+2)*1+0)*2+1)*3+1

Suppose v = [2 2 1 3 3 3 1 2 3] contains the nine bases, that is the nine lengths in the cell array. Suppose N = 556. You can obtain these nine digits with a for-loop:

 q = N;
 for k = 9:-1:1
  r = mod(q,v(k));
  q = (q-r)/v(k);
  % At this point use r+1 as an index into the k-th cell array
  % of possible numbers and place result in a matrix, X(N+1,k)
 end

  You would have an outer for-loop going through all the possible values of N from 0 to 647. I'll let you work out the rest of the details.

  The only difference between the above and converting a number to decimal or binary digits is that the bases are variable in this situation.

Roger Stafford

Subject: looking at all possible combinations

From: Bruno Luong

Date: 10 Jul, 2010 09:03:05

Message: 12 of 12

"Ozge Taskan" <lordgy@yahoo.com> wrote in message <i18p6g$kri$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hsrrsd$o20$1@fred.mathworks.com>...
> > I see. If Jos is correct, then you can also do this with a slight modification of the solution I gave earlier.
> >
> >
> > [m,n] = size(A);
> > Rr = npermutek(1:n,m); % The "Row-Rank" matrix.
> > G = A((Rr-1)*m+repmat(1:m,n^m,1));
> >
> > IIRC, COMBN and NPERMUTEK do the same thing using different methods.
>
> Hi,
> I would like to ask something related to same thing.
> If we have 9 numbers and we would like to assign one of the numbers below how can we look at each possibilities for example
> 1st number can be 1 or 4
> 2nd number can be 1 or 4
> 3rd number can be 4
> 4th number can be 1 or 2 or 3
> 5th number can be 1 or 2 or 3
> 6th number can be 1 or 2 or 3
> 7th number can be 3
> 8th number can be 2 or 3
> 9th number can be 1 or 2 or 3
> how can we construct 2*2*1*3*3*3*1*2*3(648) numbers?
>

You can use NDGRID as following:

c={[1 4] [1 4] 4 [1 2 3] [1 2 3] [1 2 3] 3 [2 3] [1 2 3]}

n = length(c);
[c{:}]=ndgrid(c{:});
c=cat(n+1,c{:});
c = reshape(c,[],n) % 648 combinations of 9 numbers

% 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