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 件)
S = dec2bin((1:32).') % If a string is o.k.
N = S - '0' % If you need a numeric matrix.
9 件のコメント
John Petersen
2012 年 12 月 17 日
How does the N=S-'0' work? I see that it does, very clever, but why?
JRC
2012 年 12 月 17 日
@JRC: Add:
N(N==0) = -1;
Please accept an answer if it helps you.
Azzi Abdelmalek
2012 年 12 月 17 日
%or
N(~N)=-1;
JRC
2012 年 12 月 17 日
John, MATLAB converts both strings to doubles then subtracts. It is the same as doing:
S - 48
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").
Rabiya Tanveer
2021 年 8 月 15 日
helpful
S = (dec2bin((1:32)) - '0') * 2 - 1
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!