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.

답변 (1개)

Matt Fig
Matt Fig 2012년 12월 17일
편집: Matt Fig 2012년 12월 17일

0 개 추천

S = dec2bin((1:32).') % If a string is o.k.
N = S - '0' % If you need a numeric matrix.

댓글 수: 9

How does the N=S-'0' work? I see that it does, very clever, but why?
JRC
JRC 2012년 12월 17일
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 2012년 12월 17일
편집: José-Luis 2012년 12월 17일
@JRC: Add:
N(N==0) = -1;
Please accept an answer if it helps you.
%or
N(~N)=-1;
JRC
JRC 2012년 12월 17일
Ok...
Thanks.
Matt Fig
Matt Fig 2012년 12월 17일
편집: Matt Fig 2012년 12월 17일
John, MATLAB converts both strings to doubles then subtracts. It is the same as doing:
S - 48
Image Analyst
Image Analyst 2012년 12월 17일
편집: Image Analyst 2012년 12월 17일
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").
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

JRC
2012년 12월 17일

댓글:

2021년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by