About matrix: how to make matrix binary (0 and 1)

I need write the matrix (0 0 0 0 0), (1 0 0 0 0), (1 1 0 0 0), (1 1 1 0 0),..., (1 1 1 1 1) with 32 possibilities of zero and one using the matlab, and don´t hand.
Thanks.

Respuestas (1)

Matt Fig
Matt Fig el 17 de Dic. de 2012
Editada: Matt Fig el 17 de Dic. de 2012
S = dec2bin((1:32).') % If a string is o.k.
N = S - '0' % If you need a numeric matrix.

9 comentarios

John Petersen
John Petersen el 17 de Dic. de 2012
How does the N=S-'0' work? I see that it does, very clever, but why?
JRC
JRC el 17 de Dic. de 2012
Thanks Matt Fig...
And it´s possible to do the matrix (-1 -1 -1 -1 -1), (-1 -1 -1 -1 1), (-1 -1 -1 1 1), (-1 -1 1 1 1),..., (1 1 1 1 1) with 32 possibilities of one and one minus using the matlab, and don´t hand.
José-Luis
José-Luis el 17 de Dic. de 2012
Editada: José-Luis el 17 de Dic. de 2012
@JRC: Add:
N(N==0) = -1;
Please accept an answer if it helps you.
Azzi Abdelmalek
Azzi Abdelmalek el 17 de Dic. de 2012
%or
N(~N)=-1;
JRC
JRC el 17 de Dic. de 2012
Ok...
Thanks.
Matt Fig
Matt Fig el 17 de Dic. de 2012
Editada: Matt Fig el 17 de Dic. de 2012
John, MATLAB converts both strings to doubles then subtracts. It is the same as doing:
S - 48
Image Analyst
Image Analyst el 17 de Dic. de 2012
Editada: Image Analyst el 17 de Dic. de 2012
You can also cast to logical if you want a boolean type of variable:
logicalVariable = logical(doubleVariable);
Logical variables are useful in indexing, especially in image processing. In image processing a "binary" variable is usually assumed to be of logical type ("class").
Rabiya Tanveer
Rabiya Tanveer el 15 de Ag. de 2021
helpful
S = (dec2bin((1:32)) - '0') * 2 - 1
S = 32×6
-1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 1 -1 -1 -1 1 1 -1 -1 -1 -1 1 1 1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 1 -1 -1 1 -1 1 -1

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

JRC
el 17 de Dic. de 2012

Comentada:

el 15 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by