axcelenator
axcelenator

Reputation: 1527

Convert a matrix of type char to type logical in matlab

Hello I have a char matrix that represents a binary numbers. For example:

0000
1010
0111
.
.
.
1010

How can I convert it to a logical matrix?

Upvotes: 1

Views: 421

Answers (3)

Anatch
Anatch

Reputation: 453

Try

b_bin = logical(b(:)'-'0')

if b is the name of your matrix.

Upvotes: 0

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

You can compare it with character '1'

>> A=['0101';'1011']
A =
0101
1011
>> A=='1'
ans =
     0     1     0     1
     1     0     1     1

Upvotes: 4

SamuelNLP
SamuelNLP

Reputation: 4136

Is this what you want?

a = {'0000'; '1010'; '0111'};
b = logical(double(cell2mat(a)) - 48);

gives,

>> b

b =

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

>> class(b)

ans =

logical

Upvotes: 1

Related Questions