Convert logical values into numeric values

509 visualizaciones (últimos 30 días)
Maria
Maria el 19 de Ag. de 2014
Comentada: Adesanya Taiwo el 2 de Mzo. de 2024
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.

Respuesta aceptada

Star Strider
Star Strider el 19 de Ag. de 2014
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.

Más respuestas (3)

Jonatan Tidare
Jonatan Tidare el 26 de Mzo. de 2018
double(A) works

chaman lal dewangan
chaman lal dewangan el 13 de Mzo. de 2018
Editada: chaman lal dewangan el 13 de Mzo. de 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 comentarios
James Tursa
James Tursa el 13 de Mzo. de 2018
Editada: James Tursa el 13 de Mzo. de 2018
This doesn't work either. Have you tried it?
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Julotek
Julotek el 21 de Mzo. de 2018
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.

Iniciar sesión para comentar.


Jonathan Patao
Jonathan Patao el 12 de Mzo. de 2018
Editada: Jonathan Patao el 13 de Mzo. de 2018
Try this:
B = cell2mat(A).*1;
  4 comentarios
Jonathan Patao
Jonathan Patao el 13 de Mzo. de 2018
you're right, my fault. you need to convert from cell first. try:
B = cell2mat(A).*1;
Star Strider
Star Strider el 13 de Mzo. de 2018
... duplicating my Answer!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by