create matrix with all possible combinations

View: New views
5 Messages — Rating Filter:   Alert me  

create matrix with all possible combinations

by xeon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I would like to create a matrix with 8 columns with all possible combinations with the values 0 and 1 in Octave.

For example,
[0,0,0,0,0,0,0,0;1,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0;...;1,1,1,1,1,1,1,1];

How can I do that?

thanks,

Re: create matrix with all possible combinations

by Joshua Stults :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Nov 2, 2009 at 7:43 PM, xeon <psdc1978@...> wrote:

>
> Hi,
>
> I would like to create a matrix with 8 columns with all possible
> combinations with the values 0 and 1 in Octave.
>
> For example,
> [0,0,0,0,0,0,0,0;1,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0;...;1,1,1,1,1,1,1,1];
>
> How can I do that?

Perhaps nchoosek() will give you what you want:

  n = [ones(8,1); zeros(8,1)];
  A = nchoosek(n, 8);

The size of a matrix created this way can grow very large very
quickly; calling nchoosek() with a scalar will give you the number of
combinations:

nchoosek(16, 8)
ans = 12870


--
Joshua Stults
Website: http://j-stults.blogspot.com
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: create matrix with all possible combinations

by Ben Abbott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Nov 2, 2009, at 7:43 PM, xeon wrote:

> Hi,
>
> I would like to create a matrix with 8 columns with all possible
> combinations with the values 0 and 1 in Octave.
>
> For example,
> [0,0,0,0,0,0,0,0;1,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0;...;1,1,1,1,1,1,1,1];
>
> How can I do that?
>
> thanks,

The may be a better way, but you can accomplish what you want by ...

        n = 8;
        a = dec2bin ([0:2^n-1], n);
        b = num2cell (a(:), 2);
        c = cellfun (@(x) eval(x), b);
        d = reshape (c, [2^n, n])

Ben
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: create matrix with all possible combinations

by Thomas Shores-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There are probably faster, simpler ways, but here's one:

n = 8;
x = (0:2^n-1)';
M = zeros(2^n,n);
for k = 1:n
   M(:,k) = mod(x,2);
   x = (x-M(:,k))/2;
end;



On 11/02/2009 06:43 PM, xeon wrote:

> Hi,
>
> I would like to create a matrix with 8 columns with all possible
> combinations with the values 0 and 1 in Octave.
>
> For example,
> [0,0,0,0,0,0,0,0;1,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0;...;1,1,1,1,1,1,1,1];
>
> How can I do that?
>
> thanks,
>
>    

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: create matrix with all possible combinations

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Nov 3, 2009 at 1:43 AM, xeon <psdc1978@...> wrote:

>
> Hi,
>
> I would like to create a matrix with 8 columns with all possible
> combinations with the values 0 and 1 in Octave.
>
> For example,
> [0,0,0,0,0,0,0,0;1,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0;...;1,1,1,1,1,1,1,1];
>
> How can I do that?
>
> thanks,
>

With 3.3.50+, it's easy:

reshape (bitunpack (uint8(0:255)), 8, []).'

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave