From: Dave Smith on
How do I put a binary value into a matrix so I can read each bit one by one? for example B=input('enter a binary value: ') so if B = 101101 then a matrix containing [1 0 1 1 0 1] and how would I read each bit of that matrix or is there a better way to do this? I'm trying to do a encoding scheme graphs so I would like to able able to read each bit and if it's a 1 it goes high or 0 it goes low for example.
From: Walter Roberson on
Dave Smith wrote:
> How do I put a binary value into a matrix so I can read each bit one by
> one? for example B=input('enter a binary value: ') so if B = 101101 then
> a matrix containing [1 0 1 1 0 1] and how would I read each bit of that
> matrix or is there a better way to do this? I'm trying to do a encoding
> scheme graphs so I would like to able able to read each bit and if it's
> a 1 it goes high or 0 it goes low for example.

Tell input that you expect a string rather than a number. Verify that
the string contains only '0' and '1', e.g.,
if all(ismember(B,['0' '1'])) %string is okay
Then convert the string to a number matrix by subtracting '0' from the
string; e.g., Bnumeric = B - '0'. Or alternately, to save space,
Blogical = B == '1'; will use false (numeric value 0) for '0' and will
use true (numeric value 1) for '1'.