How to create a binary matrix

I want to make matrix whose each element's size will be 1 bit, i.e, it will be a binary matrix.How can I do it?
Thanks in advance.

回答 (5 件)

Jan
Jan 2011 年 7 月 20 日

1 投票

There is not BIT type in Matlab. Usually binary arrays are stored as LOGICAL, which are equivalent to UINT8. E.g.:
B = rand(16, 16) > 0.8;
whos('B')
Now you can pack this in blocks of 8 elements to a UINT8 array:
T = 2 .^ (0:7).';
B8 = transpose(reshape(B, 8, []));
BitArray = uint8(B8 * T);
Friedrich
Friedrich 2011 年 7 月 20 日

0 投票

Hi,
this is not possible in MATLAB and in other languages too like C or C++. In modern computer architectures, a byte is the smallest addressable unit of memory. To pack multiple bits into a byte requires applying extra bit-shift operations. At the compiler level, it's a trade off of memory vs. speed requirements (and in high-performance software, those extra bit-shift operations can add up and slow down the application needlessly).

3 件のコメント

snake eyes
snake eyes 2011 年 7 月 20 日
C has a feature known as 'bit field' where I can specify custom length.
however,thanks for the reply.
Walter Roberson
Walter Roberson 2011 年 7 月 20 日
C does not specify whether bit-fields start from the beginning or the end of a word, and C permits but does not require bit-fields to cross words. In C, arrays of bit-fields are arrays of words: the "packed" attribute possibility was removed from the C language before C was standardized. There is thus no binary matrix in C, just arrays of structs whose members are bit fields.
Walter Roberson
Walter Roberson 2011 年 7 月 20 日
Well, in _popular_ computer architectures; bit-sliced and bit-addressable architectures still have their purposes.

サインインしてコメントする。

Sean de Wolski
Sean de Wolski 2011 年 7 月 20 日

0 投票

To get logical binary values either use:
x = logical(x)
or any conditional operator e.g.
x = x>7
x = x==11;
Image Analyst
Image Analyst 2017 年 5 月 22 日
編集済み: Image Analyst 2017 年 5 月 22 日

0 投票

Use true():
t = true(numRows, numColumns);
"whos" will show you it's one byte per element, like the other solutions.
>> t = true(4,5)
t =
4×5 logical array
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
>> whos t
Name Size Bytes Class Attributes
t 4x5 20 logical
To get one bit per element, instead of one byte per element, I think you'll have to pack them together yourself.
Ekta Gujral
Ekta Gujral 2017 年 7 月 19 日

0 投票

A = randi([0 1], n,m)

1 件のコメント

Walter Roberson
Walter Roberson 2017 年 7 月 19 日
This produces an array of double, not bits.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2011 年 7 月 20 日

コメント済み:

2017 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by