MATLAB Answers

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

28 views (last 30 days)
JRC
JRC on 17 Dec 2012
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.

  0 Comments

Sign in to comment.

Answers (1)

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

  7 Comments

Show 4 older comments
Matt Fig
Matt Fig on 17 Dec 2012
John, MATLAB converts both strings to doubles then subtracts. It is the same as doing:
S - 48
Image Analyst
Image Analyst on 17 Dec 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").

Sign in to comment.

Sign in to answer this question.