Zeta.Investigator
Zeta.Investigator

Reputation: 973

How to convert '1*1 char' to a logical vector in MATLAB?

Actually I'm trying to convert hex to bin.

a=hex2dec('ab32');  
a=dec2bin(a);
%now I have a 1to1 char array of for example 1010101...
%I want to have an 1*16 array of 1 and 0's

How can I do this?

Upvotes: 2

Views: 709

Answers (2)

High Performance Mark
High Performance Mark

Reputation: 78334

This gives you a 1*16 vector of reals, all either 0 or 1:

(dec2bin(hex2dec('ab32'))-'0')

while this gives you a 1*16 vector of logicals, all either false or true (which look like 0s and 1s)

(dec2bin(hex2dec('ab32'))-'0')==1

Upvotes: 1

Paul R
Paul R

Reputation: 213039

You can do this:

a=logical(a-'0')

Example:

octave:224> a=hex2dec('ab32')
a =  43826
octave:225> a=dec2bin(a)
a = 1010101100110010
octave:226> a=logical(a-'0')
a =

   1   0   1   0   1   0   1   1   0   0   1   1   0   0   1   0

octave:227> whos a
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        a           1x16                        16  logical

Total is 16 elements using 16 bytes

octave:228> 

Upvotes: 5

Related Questions