Convert logical values into numeric values

I have a cell array 20000 rows and 20 columns with logical values:
A={0 0 0 0 1 0...
0 0 1 0 0 0
1 0 0 0 0 0
0 1 0 0 0 0 ...}
And I would like to convert this array into numeric values of 1 and 0. Can someone help me? Thank you.

 Risposta accettata

This works:
B = double(cell2mat(A));
The cell2mat call converts it from a cell to a logical array, and double converts it to a double array.

Più risposte (3)

chaman lal dewangan
chaman lal dewangan il 13 Mar 2018
Modificato: chaman lal dewangan il 13 Mar 2018
I wrote small code
for a=1:number of rows
for b=1:number of columns
if A(a,b)==true
new_matrix(a,b)=1;
else
new_matrix(a,b)=0;
end
end
end

2 Commenti

This doesn't work either. Have you tried it?
??? Undefined function or method 'eq' for input arguments of type 'cell'.
for a=1:number of rows
for b=1:number of columns
if A{a,b}
new_matrix(a,b)=1;
else
new_matrix(a,b)=0;
end
end
end
That should work but it is maybe too complicated for you need.

Accedi per commentare.

Jonathan Patao
Jonathan Patao il 12 Mar 2018
Modificato: Jonathan Patao il 13 Mar 2018
Try this:
B = cell2mat(A).*1;

4 Commenti

I just did. It doesn't work:
>> A = {1 0;0 1}
A =
[1] [0]
[0] [1]
>> B = A.*1;
Undefined operator '.*' for input arguments of type 'cell'.
@James —
Yours confirms my result:
A = {[1 0 1 1 0] == 1};
B = A.*1;
It throws the same error.
you're right, my fault. you need to convert from cell first. try:
B = cell2mat(A).*1;
... duplicating my Answer!

Accedi per commentare.

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by