Constuctiing special array (like binary)

Hi, I'am new to matlab and I want to make an nxn matrix containing as first row zeros and as last row ones. The other rows should be like binary digits.
Example with n=4:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Thank you!!

2 comentarios

Matt J
Matt J el 4 de Dic. de 2013
The matrix you've shown is not nxn. It is 2^n x n.
Dimitris Sideratos
Dimitris Sideratos el 5 de Dic. de 2013
You are right! Thank you!

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 4 de Dic. de 2013
dec2bin(0:2^n-1)-'0'

3 comentarios

Dimitris Sideratos
Dimitris Sideratos el 4 de Dic. de 2013
thank you!!
Matt J
Matt J el 19 de En. de 2014
Editada: Matt J el 19 de En. de 2014
More generally
m=5; n=3;
[c{1:n}]=ndgrid(1:m);
result=reshape( cat(n+1,c{end:-1:1}),m^n,n )
Dimitris Sideratos
Dimitris Sideratos el 19 de En. de 2014
Thank you!

Iniciar sesión para comentar.

Más respuestas (3)

Andrei Bobrov
Andrei Bobrov el 4 de Dic. de 2013
Editada: Andrei Bobrov el 4 de Dic. de 2013
rem(floor((0:pow2(n)-1)'*pow2(1-n:0)),2)
Dimitris Sideratos
Dimitris Sideratos el 19 de En. de 2014

0 votos

Can I extend this script to run with more than 2 values (0 or 1)?? I want m^n x n an the values should be 0,1.. m-1??
Thank you in advance!

2 comentarios

Jan
Jan el 19 de En. de 2014
Please open a new thread for a new question.
Dimitris Sideratos
Dimitris Sideratos el 20 de En. de 2014
Ok. You are right.

Iniciar sesión para comentar.

Jos (10584)
Jos (10584) el 20 de En. de 2014
Editada: Jos (10584) el 20 de En. de 2014
Take a look at COMBN:
USe like this:
N = 2 ; M = 3 ;
combn(0:M-1,N)

Categorías

Etiquetas

Preguntada:

el 4 de Dic. de 2013

Comentada:

el 21 de En. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by