Asked by snake eyes
on 20 Jul 2011

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.

*No products are associated with this question.*

Answer by Jan Simon
on 20 Jul 2011

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);

Answer by Friedrich
on 20 Jul 2011

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).

snake eyes
on 20 Jul 2011

however,thanks for the reply.

Walter Roberson
on 20 Jul 2011

Walter Roberson
on 20 Jul 2011

Answer by Sean de Wolski
on 20 Jul 2011

To get logical binary values either use:

x = logical(x)

or any conditional operator e.g.

x = x>7 x = x==11;

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments